12

मैं एमवीसी 5 ओविन लॉगिन के लिए दावा सीखने की कोशिश कर रहा हूं। मैंने इसे यथासंभव सरल रखने की कोशिश की। मैंने एमवीसी टेम्पलेट के साथ शुरुआत की और मेरे दावे कोड डाले (नीचे देखें)। जब मैं दृश्य में @ Html.AntiForgeryToken() सहायक का उपयोग करता हूं तो मुझे एक त्रुटि मिलती है।दावों और एंटीफॉर्गेरी टोकन के साथ एमवीसी 5 ओविन लॉगिन। क्या मुझे दावा दावेदार प्रदाता याद आती है?

त्रुटि:

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: /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> 
    } 
} 

मैं tryed है सेटिंग 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; 
} 

और फिर मैं "केवल?" इस त्रुटि

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

मैं antiforgeryToken रखने के लिए, क्योंकि यह क्रॉस-साइट स्क्रिप्टिंग के खिलाफ मदद कर सकते हैं चाहते।

उत्तर

13

आपका दावा पहचान ClaimTypes.NameIdentifier नहीं है, तो आप दावे सरणी में और अधिक जोड़ने चाहिए:

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

जानकारी नक़्शा बनाने के लिए अधिक सुधारात्मक के लिए दावा करने के लिए:

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

के बाद से उपयोगकर्ता नाम भी अद्वितीय है, इसलिए आप विरोधी जालसाजी टोकन समर्थन के लिए username का उपयोग करने में सक्षम हैं।

+1

मुझे नहीं पता कि मानचित्रण क्या करता है। सबसे पहले मैंने 'NameIdentifier' भी डाला लेकिन मुझे एक ही त्रुटि मिली। फिर मैंने यह जानने के बिना Global.asax.cs में जोड़ने का प्रयास किया, लेकिन अब यह काम करता है। शायद यह आपके मैपिंग जैसा ही है? ग्लोबल.एएसएक्स सीएस .: एंटीफोर्गेरी कॉनफिग। यूनिकक्लेम टाइप टाइप इंडेंटिफ़ायर = दावा टाइप्स नाम; एंटीफोर्गेरीकोनफिग। यूनिकक्लेम टाइप टाइप इंडेंटिफ़ायर = दावा टाइप्स.ईमेल; एंटीफोर्गेरीकोनफिग। यूनिकक्लेम टाइप टाइप इंडेंटिफ़ायर = दावा टाइप्स.NameIdentifier; – radbyx

12

अपने Application_Start() में, जो Claim के रूप में उपयोग करने के लिए निर्दिष्ट NameIdentifier:

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

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

     ... 
    } 
} 

देखें: http://brockallen.com/2012/07/08/mvc-4-antiforgerytoken-and-claims/

+0

धन्यवाद ट्रैविस, यह मेरी समस्या को तुरंत ठीक कर दिया .... ऑटो 0 को OWIN Asp.net पर लागू करते समय इसका अनुभव किया –

3

AntiForgeryConfig यह अन्य उपयोग करने के लिए AntiForgeryConfig स्थापित करने के लिए है हल करने के लिए

एक ही रास्ता 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; 
} 

NameIdentifier और IdentityProvider ClaimTypes

वैकल्पिक रूप से जोड़े, आप अपने दावे के लिए NameIdentifier और IdentityProvider ClaimTypes जोड़ सकते हैं।

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) 
}) 

देखें:

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

1

मैं Global.asax.cs Application_Start पर इस प्रयोग किया जाता है() और त्रुटि को हल किया।नाम;

0

मेरे पास इसी तरह का मुद्दा था जो कुकी से संबंधित हो गया; मैं एक साथ दो एमवीसी साइट्स विकसित कर रहा था और क्योंकि एएसपी.Net साइटें डिफ़ॉल्ट रूप से एक ही कुकी नाम का उपयोग करती हैं, दोनों साइटें एक दूसरे के साथ हस्तक्षेप कर रही थीं। कुकीज़ को साफ़ करने से समस्या ठीक हो गई। मेरे answer here में इस पर और कुछ है।

संबंधित मुद्दे