Is there a way to create a class in ASP.NET identity that gives me the current user who is logged into my system? I need all of his details
instead of call the User Manager each time I want to access to its details.
I am trying to make a class to give all the details of the logged in user in ASP.NET identity.
2
Answers
Your question is not really clear 🙁
Do you want to store additional user information in the ClaimsIdentity when the user logs in? That can be achieved by a class implementing the IClaimsTransformation interface. I would not recommend writing sensitive information into the claims though.
If you want to access the user’s information, you can use the HttpContext.User. The current HttpContext can be accessed via the IHttpContextAccessor.
According to your description, I suggest you could add the user infomration inside the claims instead of creating a new instance inside your application. But please notice, we don’t suggest you store the importent user information inside the claims, since the claims will be stored as cookie inside user side. By using claims, we could get more information for that user.
More details about how to do it, you could refer to below codes:
Crete a UserClaimsPrincipalFactory and register it inside the DI.
Inside the program.cs
Result: