12

Sto provando ad apprendere i reclami per l'accesso OWIN di MVC 5. Ho cercato di mantenerlo il più semplice possibile. Ho iniziato con il modello MVC e inserito il mio codice sinistri (vedi sotto). Viene visualizzato un errore quando utilizzo l'helper @ Html.AntiForgeryToken() nella vista.MVC 5 Login OWIN con attestazioni e AntiforgeryToken. Mi manca un fornitore di ClaimsIdentity?

Errore:

A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' or 
'http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovid  
er' 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. 

Exception Details: System.InvalidOperationException: 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. 

Source Error: 
Line 4:  using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new 
{ id = "logoutForm", @class = "navbar-right" })) 
Line 5:  { 
Line 6:  @Html.AntiForgeryToken() 

azione POST Accesso

// POST: /Account/Login 
[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
{ 
    if (!ModelState.IsValid) 
    { 
     return View(model); 
    } 

    var claims = new List<Claim> 
    { 
     new Claim(ClaimTypes.Name, "Brock"), 
     new Claim(ClaimTypes.Email, "[email protected]") 
    }; 
    var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); 

    var ctx = Request.GetOwinContext(); 
    var authenticationManager = ctx.Authentication; 
    authenticationManager.SignIn(id); 

    return RedirectToAction("Welcome"); 
} 

_LoginPartial.cshtml

@using Microsoft.AspNet.Identity 
@if (Request.IsAuthenticated) 
{ 
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" })) 
    { 
    @Html.AntiForgeryToken() 

    <ul class="nav navbar-nav navbar-right"> 
     <li> 
      @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" }) 
     </li> 
     <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li> 
    </ul> 
    } 
} 

Ho provato impostazione ClaimTypes.NameIdentifier (like in this SO answer)

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 

    AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier; 
} 

E poi ho "solo?" questo errore

A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' was 
not present on the provided ClaimsIdentity. 

voglio mantenere l'antiforgeryToken perché può aiutare contro cross-site scripting.

risposta

13

tuo attestazione di identità non ha ClaimTypes.NameIdentifier, si dovrebbe aggiungere altro in una matrice affermazione:

var claims = new List<Claim> 
{ 
    new Claim(ClaimTypes.Name, "username"), 
    new Claim(ClaimTypes.Email, "[email protected]"), 
    new Claim(ClaimTypes.NameIdentifier, "userId"), //should be userid 
}; 

mappare le informazioni rivendicazione per più correttiva:

ClaimTypes.Name => map to username 
ClaimTypes.NameIdentifier => map to user_id 

Dal nome utente è unico anche, quindi sei in grado di utilizzare username per il supporto token anti-contraffazione.

+1

Non so cosa faccia la mappatura. Per prima cosa ho inserito anche il 'NameIdentifier', ma ho ottenuto lo stesso errore. Poi ho provato ad aggiungere in Global.asax.cs senza sapere cosa fa, ma ora funziona. Forse è lo stesso della tua mappatura? Global.asax.cs .: AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Name; AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Email; AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier; – radbyx

12

Nella tua Application_Start(), specificare quale Claim da utilizzare come NameIdentifier:

public class MvcApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     ... 

     System.Web.Helpers.AntiForgeryConfig.UniqueClaimTypeIdentifier = 
      System.Security.Claims.ClaimTypes.NameIdentifier; 

     ... 
    } 
} 

See: http://brockallen.com/2012/07/08/mvc-4-antiforgerytoken-and-claims/

+0

Grazie a Travis, questo ha risolto subito il mio problema .... L'ho provato applicando Auto 0 a OWIN Asp.net –

3

AntiForgeryConfig

Un modo per risolverlo è quello di impostare AntiForgeryConfig di utilizzare altri ClaimType.

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 

    WebApiConfig.Register(GlobalConfiguration.Configuration); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 

    AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Email; 
} 

Aggiungi NameIdentifier e IdentityProvider ClaimTypes

In alternativa, è possibile aggiungere NameIdentifier e IdentityProvider ClaimTypes alle vostre richieste.

List<Claim> _claims = new List<Claim>(); 
_claims.AddRange(new List<Claim> 
{ 
    new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", _user.Email)), 
    new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", _user.Email) 
}) 

Vedi: https://stack247.wordpress.com/2013/02/22/antiforgerytoken-a-claim-of-type-nameidentifier-or-identityprovider-was-not-present-on-provided-claimsidentity/

1

Ho usato questo su Global.asax.cs Application_Start() e risolto l'errore:

AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Nome;

0

Ho riscontrato un problema simile a questo che si è rivelato correlato ai cookie; Stavo sviluppando due siti MVC contemporaneamente e poiché i siti ASP.Net utilizzano tutti lo stesso nome di cookie, per impostazione predefinita i due siti interferivano tra loro. La cancellazione dei cookie ha risolto il problema. C'è altro su questo nel mio answer here.

Problemi correlati