What is authentication in ASP.NET Core, and how does the framework represent an authenticated user?
Short answer
Authentication is the process of establishing a user's identity, resulting in a ClaimsPrincipal (accessible via HttpContext.User) populated with claims describing that user -- name, roles, and any custom attributes -- once an authentication handler successfully validates the…
Advertisement
AModel answer
Authentication is the process of establishing a user's identity, resulting in a ClaimsPrincipal (accessible via HttpContext.User) populated with claims describing that user -- name, roles, and any custom attributes -- once an authentication handler successfully validates the request's credentials (a cookie, JWT, or other scheme-specific mechanism).
builder.Services.AddAuthentication("Cookies").AddCookie();
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/me", (HttpContext ctx) => ctx.User.Identity?.Name);Real-world example
After a user logs in and a cookie authentication handler validates their session cookie on a subsequent request, HttpContext.User.Identity.IsAuthenticated becomes true and the rest of the pipeline can access their claims.
Likely follow-ups
- What's the difference between authentication and authorization?
- What happens to HttpContext.User for an anonymous, unauthenticated request?
Related topics