Junior — Middle
Can you describe in detail the authentication and authorization process used in your current project?
sobes.tech AI
Answer from AI
In my current C# project, the authentication and authorization process is built as follows:
-
Authentication — verifying the user's identity. We use JWT (JSON Web Tokens). When logging in, the user enters their login and password, the server verifies them, and if the data is correct, it generates a JWT with user information and an expiration time.
-
Authorization — checking access rights. The JWT contains claims (user rights), which are checked with each request to protected resources. Based on these claims, access to methods and data is controlled.
An example of simplified token validation code in ASP.NET Core:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters {
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidIssuer = "MyIssuer",
ValidAudience = "MyAudience",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SecretKey"))
};
});
[Authorize]
public IActionResult GetData() {
// Access is only for authenticated users
}
Thus, authentication confirms who the user is, and authorization — what they are allowed to do.