I am trying to find a document or example of how you would add custom claims to the user identity in MVC 5 using ASP.NET Identity. The example should show where to insert the claims in the OWIN security pipeline and how to persist them in a cookie using forms authentication.
C# – How to add claims in ASP.NET Identity
asp.net-identityasp.net-mvc-5c
Best Answer
The correct place to add claims, assuming you are using the ASP.NET MVC 5 project template is in
ApplicationUser.cs
. Just search forAdd custom user claims here
. This will lead you to theGenerateUserIdentityAsync
method. This is the method that is called when the ASP.NET Identity system has retrieved an ApplicationUser object and needs to turn that into a ClaimsIdentity. You will see this line of code:After that is the comment:
And finally, it returns the identity:
So if you wanted to add a custom claim, your
GenerateUserIdentityAsync
might look something like: