Introduction
In a modern web application, allowing users to sign in is not enough. The application must also reliably determine who is making a request, which operations that user may perform, and which data the user may access. This lesson introduces the security foundation of ASP.NET Core applications: Authentication and Authorization.
Authentication identifies and verifies a user. It answers the question, “Who is making this request?” Authorization evaluates whether the authenticated user is allowed to access a particular resource or perform a particular operation. It answers the question, “What is this user allowed to do?”
For example, when a user submits an email address and password, the Authentication process begins. The password is validated, an identity is created, and that identity becomes available through HttpContext.User for the current request. If the user then opens an administration page, Authorization checks whether the user has the administrator role or another required permission.
Understanding this distinction is essential before implementing ASP.NET Core Identity, Cookie Authentication, or JWT Bearer Authentication in later lessons.
Authentication versus Authorization
| Concept | Main question | Example |
|---|---|---|
| Authentication | Who is this user? | Is the password or JWT valid? |
| Authorization | What may this user do? | Can the user delete a report? |
If Authentication fails, the application cannot establish a trusted user identity. Authorization can still deny access even when Authentication succeeds. Therefore, being signed in does not mean that a user can access every resource in the application.
Suppose Alex and Taylor are both signed-in users. Both may pass Authentication successfully. However, Alex may have the Admin role and access the user-management area, while Taylor may only view their own profile. This difference is enforced by Authorization rules.
The ASP.NET Core security pipeline
ASP.NET Core processes incoming HTTP requests through a middleware pipeline. Each middleware component can inspect a request, modify it, stop processing, or pass it to the next component. Authentication and Authorization middleware must be placed in an appropriate order.
var builder = WebApplication.CreateBuilder(args);builder.Services.AddAuthentication();
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();UseAuthentication attempts to identify the caller. The application may use a cookie, bearer token, or another authentication scheme. If the credentials are valid, authentication creates a ClaimsPrincipal and assigns it to HttpContext.User.
UseAuthorization evaluates the access rules associated with the selected endpoint. It normally follows UseAuthentication so that the user identity and its claims are available when authorization decisions are made.
Authentication middleware does not necessarily reject every anonymous request by itself. It attempts to establish an identity. Access is usually denied later when an endpoint marked with [Authorize] or another authorization rule is evaluated.
What is ASP.NET Core Identity?
ASP.NET Core Identity is a framework system for managing user accounts and security-related user data. In addition to registration and login, it supports password management, roles, claims, token providers, email confirmation, password recovery, and related account operations.
Identity commonly stores user data in a relational database. When combined with Entity Framework Core and SQL Server, it can create tables for users, roles, claims, and their relationships. Identity should not be viewed only as a prebuilt login screen. Its services also provide standardized password hashing, user management, role management, and account workflows.
A useful distinction is that Identity manages the account, while Authentication identifies that account during an HTTP request. Identity can create and manage a user. A cookie or token authentication scheme can then represent that user on subsequent requests.
Users, roles, and claims
User
A user is an identified actor in the application. A user record may contain an email address, a password hash, a display name, account status, and verification information. The user model answers the question, “Which account is this?”
Role
A role groups users with similar access requirements. Common examples include Admin, Editor, and Customer. Role-based authorization checks whether the current user belongs to a required role.
[Authorize(Roles = "Admin")]
public IActionResult DeleteUser(int id)
{
return Ok();
}This endpoint is available only to users in the Admin role. Roles are useful for broad categories of access, but creating too many roles can make administration difficult. For fine-grained permissions, claims or policies may be a better fit.
Claim
A claim is a name-value piece of information associated with an identity. Examples include department=finance, tenantId=42, and permission=reports.read.
A claim does not automatically mean “the user is allowed to perform this operation.” It describes the user or client. The application evaluates the claim through an authorization rule and then makes an access decision. An identity may contain multiple claims and may contain multiple values for the same claim type.
Policy
A policy packages authorization rules into a named, reusable definition. A policy can contain one or more requirements. A requirement represents a condition that the current user must satisfy.
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("ReportsRead", policy =>
{
policy.RequireClaim("permission", "reports.read");
});
});[Authorize(Policy = "ReportsRead")]
public IActionResult GetReports()
{
return Ok();
}This approach keeps security rules centralized instead of scattering string comparisons throughout controllers. Later lessons will cover custom authorization requirements and handlers in more detail.
Sessions, cookies, and tokens
Session helps a server store temporary state associated with a client. A shopping cart or short-lived user preference may be stored in session. However, session is not automatically an authentication mechanism. The application still needs a reliable way to establish the user's identity.
With Cookie Authentication, the server issues an authentication cookie after a successful login. The browser sends that cookie with later requests. The server validates the cookie and reconstructs the user identity. This approach is common in MVC and Razor Pages applications.
With Token Authentication, the client usually sends a bearer token in an HTTP header.
Authorization: Bearer eyJhbGciOi...A JWT is a signed token containing claims about a user or client. An API validates the token signature, expiration, issuer, and audience before accepting the identity represented by the token. When validation succeeds, the claims are used to create HttpContext.User.
Cookies and tokens can both carry authentication information with a request, but they have different operational and security characteristics. Cookie-based applications must consider browser behavior and cross-site request forgery. JWT-based APIs must carefully design token storage, exposure risks, expiration, and refresh-token workflows.
Step-by-step request evaluation
- The client sends a request: A browser, mobile application, or API client calls a protected endpoint.
- An authentication scheme is selected: The application may use cookies, JWT bearer authentication, or another scheme.
- Authentication runs: If the cookie or token is valid, ASP.NET Core creates a
ClaimsPrincipal. - Authorization runs: The endpoint's
[Authorize], role, or policy requirements are evaluated. - A result is returned: The request is accepted, challenged, or forbidden according to the authentication and authorization result.
The distinction between HTTP 401 and 403 is important when troubleshooting. A 401 response generally means that the request did not provide valid authentication credentials. A 403 response generally means that the user was recognized but does not have sufficient permission for the requested resource.
Common mistakes
- Confusing Authentication with Authorization: Signing in a user must not automatically grant administrative access.
- Using the wrong middleware order: Placing
UseAuthorizationbeforeUseAuthenticationcan prevent expected identity information from being available. - Storing passwords as plain text: Passwords must never be stored as readable database values. Use the password-hashing infrastructure provided by Identity.
- Trusting client-side checks: Hiding a button is not security. Every protected endpoint must enforce authorization on the server.
- Inconsistent role and claim casing: Values such as
Adminandadminshould not be treated as interchangeable without an explicit, consistent design. - Trusting a JWT without validation: Decoding a token is not validation. The signature, lifetime, issuer, audience, and relevant claims must be checked.
- Putting sensitive data into tokens: Passwords, private keys, and unnecessary personal data should not be included in cookies or tokens.
Short practice exercise
Design the authorization model for a reporting API with these endpoints:
GET /api/reports: Any authenticated user may access it.POST /api/reports: Only users in theReportsWriterrole may access it.DELETE /api/reports/{id}: Only users with thereports.deleteclaim may access it.
Answer the following questions:
- Which endpoint requires only
[Authorize]? - How would you express the role requirement?
- How would you define the claim requirement as a reusable policy?
- Which HTTP status codes would you expect when no credentials are provided and when the user lacks permission?
An example policy definition is:
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("ReportsDelete", policy =>
{
policy.RequireClaim("permission", "reports.delete");
});
});The goal is not yet to build a complete login system. The goal is to place each security decision in the correct layer and choose the appropriate authorization model.
Conclusion
Authentication verifies a user's identity, while Authorization evaluates what that authenticated user may access. In the ASP.NET Core pipeline, Authentication establishes the user principal and Authorization applies the rules associated with the endpoint.
Users, roles, claims, and policies serve different purposes. A user represents an account; roles group users; claims carry information about an identity; and policies transform requirements into access decisions. Session stores temporary application state, while cookies and tokens carry authentication information across requests.
In the next lessons, we will add ASP.NET Core Identity to a project and create the user and database foundation. We will then implement registration, login, password management, roles, and authorization rules through practical examples.
Sources
- https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-10.0
- https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-10.0
- https://learn.microsoft.com/en-us/aspnet/core/authorization/introduction?view=aspnetcore-10.0
- https://learn.microsoft.com/en-us/aspnet/core/security/authorization/policies?view=aspnetcore-10.0