MVC 5 OWIN-Anmeldung mit Ansprüchen und AntiforgeryToken. Vermisse ich eine "ClaimsIdentity" - Anbieter?
Ich versuche zu lernen, Ansprüche für MVC 5 OWIN-Anmeldung. Ich Versuch ' ed halten Sie es so einfach wie möglich. Angefangen habe ich mit dem MVC-Vorlage eingefügt und meine Ansprüche-code (siehe unten). Ich erhalte eine Fehlermeldung, wenn ich den @Html.AntiForgeryToken () - Helfer in der Ansicht.
Fehler:
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()
- POST-Login-Aktion
//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>
}
}
Ich habe versucht Einstellung ClaimTypes.NameIdentifier
(wie in diesem SO beantworten)
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
}
Und ich dann "nur?" bekomme diese Fehlermeldung
A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' was
not present on the provided ClaimsIdentity.
Ich will, dass die antiforgeryToken, weil es helfen kann, gegen cross-site-scripting.
InformationsquelleAutor der Frage radbyx | 2015-06-22
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ihren Anspruch, Identität nicht
ClaimTypes.NameIdentifier
sollten Sie mehr in Anspruch array:Zuordnung der Informationen zu verlangen, die für mehr Korrekturmaßnahmen:
Da Benutzername einzigartig ist auch, so sind Sie in der Lage zu verwenden
username
für anti-forgery token-Unterstützung.InformationsquelleAutor der Antwort Cuong Le
In Ihrem
Application_Start()
geben Sie an, welcheClaim
alsNameIdentifier
:Finden Sie unter: http://brockallen.com/2012/07/08/mvc-4-antiforgerytoken-and-claims/
InformationsquelleAutor der Antwort Travis Russi
AntiForgeryConfig
Einen Weg, das zu lösen ist, um AntiForgeryConfig andere ClaimType.
Hinzufügen NameIdentifier und IdentityProvider ClaimTypes
Alternativ können Sie NameIdentifier und IdentityProvider ClaimTypes auf Ihre Ansprüche.
Finden Sie unter: https://stack247.wordpress.com/2013/02/22/antiforgerytoken-a-claim-of-type-nameidentifier-or-identityprovider-was-not-present-on-provided-claimsidentity/
InformationsquelleAutor der Antwort Alex N.
Habe ich diese auf den Globalen.asax.cs Application_Start() und löste den Fehler:
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Name;
InformationsquelleAutor der Antwort Carlos Escobosa
Ich hatte ein ähnliches Problem, stellte sich heraus, dass cookie bezogen; ich war die Entwicklung von zwei MVC-Seiten gleichzeitig und da ASP.Net Websites, die alle den gleichen cookie verwenden standardmäßig den Namen die beiden Seiten wurden sich mit jeder anderen. Löschen der cookies behoben das Problem. Es gibt noch mehr davon in meinem Antwort hier.
InformationsquelleAutor der Antwort tomRedox