I am using Asp.Net Core and ASP.NET Identity and when I get a Claim type I get something like
"type":"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
"value":"123"
How to get only the simple type name, e.g.:
"type":"nameidentifier",
"value":"123"
I know this is possible I just can't find the solution.
ClaimTypes. NameIdentifier is typically used for the user's id. In some cases it could be a username. ASP.NET Identity uses ClaimTypes.Name to store the username, and ClaimTypes. NameIdentifier to store the primary key GUID of the user.
ClaimsPrincipal exposes a collection of identities, each of which is a ClaimsIdentity. In the common case, this collection, which is accessed through the Identities property, will only have a single element.
ClaimsIdentity(IIdentity) Initializes a new instance of the ClaimsIdentity class using the name and authentication type from the specified IIdentity. ClaimsIdentity(IIdentity, IEnumerable<Claim>) Initializes a new instance of the ClaimsIdentity class using the specified claims and the specified IIdentity.
I was looking for this answer when I came across this documentation:
When you inspect the claims on the about page, you will notice two things: some claims have odd long type names and there are more claims than you probably need in your application.
The long claim names come from Microsoft’s JWT handler trying to map some claim types to .NET’s
ClaimTypes
class types. You can turn off this behavior with the following line of code (inStartup
).This also means that you need to adjust the configuration for anti-CSRF protection to the new unique sub claim type:
AntiForgeryConfig.UniqueClaimTypeIdentifier = Constants.ClaimTypes.Subject;
JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
I added this code to the Startup.cs
of my client and it shortened the claimtypes.
Update:
For newer versions of IdentityModel
, the property is called DefaultInboundClaimTypeMap
:
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
Make sure you run this line before you set up your Identity configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With