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.
This figure shows the relationship between the current principal, ClaimsPrincipal, and its collection of claims. section 23.1.1
