liveBook · Manning

HttpContext.User

As explained in ASP.NET Core in Action, 3e

HttpContext.User is the ASP.NET Core property containing the ClaimsPrincipal associated with the current HTTP request.

HttpContext.User identifies the principal associated with the current HTTP request. ASP.NET Core represents that principal with ClaimsPrincipal, which contains claims describing the user. section 23.1.1

Use the principal to:

  • Find properties such as the user’s ID, name, or email in Razor Pages and views. section 23.7
  • Inspect claims such as Name, Email, HasAdminAccess, or IsVipCustomer. section 23.1.1
  • Recognize that unauthenticated requests still have a principal: an anonymous user with no claims. section 23.1.1 section 23.7

Request-specific user information

A page can read HttpContext.User when it needs to behave differently for different requests. Before authentication, the value represents an anonymous user; after a successful login, authentication services set the principal and later requests can load it from a cookie. section 23.7

Figure 23.1 The principal is the current user, implemented as ClaimsPrincipal. It contains a collection of Claims that describe the user.
CH23_F01_Lock3

This figure shows the relationship between the current principal, ClaimsPrincipal, and its collection of claims. section 23.1.1

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines the principal and claims, explains anonymous defaults and authentication loading, but stops short of detailed claim-access code.
Examples
Uses ID, name, email, Name, Email, HasAdminAccess, and IsVipCustomer as representative user claims.
What it assumes
Assumes familiarity with HTTP requests and introduces principals, claims, authentication, and authorization.
What you can do after
Evaluate request identity and claims through HttpContext.User in Razor Pages and views.
How it connects
Connects HttpContext.User to ClaimsPrincipal, claims, authentication, authorization, cookies, and AuthenticationMiddleware.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.