Asp. Net MVC anti-forgery token: nameidentifier or identityprovider not present,
When ClaimsIdentity is used, Asp. Net MVC uses the value of ClaimsType (http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier) and IdentityProvider (http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider) in User. Identity by default when generating AntiForgeryToken ). If the STS (Security Token Service) We use does not provide two types of ClaimsType values, then MVC will report an error of generation failure of AntiForgeryToken.
The detailed error message is as follows:
A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' or 'http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider' was not present on the provided ClaimsIdentity. To enable anti-forgery token support with claims-based authentication, please verify that the configured claims provider is providing both of these claims on the ClaimsIdentity instances it generates. If the configured claims provider instead uses a different claim type as a unique identifier, it can be configured by setting the static property AntiForgeryConfig.UniqueClaimTypeIdentifier.
From the error message, we can see that Asp.net MVC forces to provide the NameIdentifier and IdentityProvider values, which is the default action. However, this default behavior can be changed. We use ADFS and IdentityProvider. According to the last prompt of the error message, you can modify the value of AntiForgeryConfig. UniqueClaimTypeIdentifier to tell Asp. Net MVC to generate an AntiForgeryToken using another ClaimsType value. For example, to use NameIdentifier, you only need to add the following sentence to Global. asax. cs:
AntiForgeryConfig. UniqueClaimTypeIdentifier = System. Security. Claims. ClaimTypes. NameIdentifier;
After re-compilation, as long as ADFS provides the NameIdentifier program, no error will be reported.
References:
- Anti-forgery token issue (MVC 5)
- MVC 4, AntiForgeryToken and Claims