1

i using claim in login . but it show me this error

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

how solve this ?

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        //AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Email;
        AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
    }
}

.

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{

    if (!ModelState.IsValid)
    {
        return View(model);
    }

    var identity =new  ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, model.Email) }, DefaultAuthenticationTypes.ApplicationCookie,
        ClaimTypes.Name, ClaimTypes.Role);
    identity.AddClaim(new Claim(ClaimTypes.Name, model.Email));
    identity.AddClaim(new Claim(ClaimTypes.Role, "Admin"));
    identity.AddClaim(new Claim(ClaimTypes.Sid, "123"));
    AuthenticationManager.SignIn(new AuthenticationProperties
    {
        IsPersistent = model.RememberMe
    }, identity);
    return RedirectToLocal(returnUrl);
}

Update

enter image description here

Mr Coder
  • 761
  • 2
  • 13
  • 34
  • 1
    You have `ClaimTypes.NameIdentifier` in `Application_Start` but use `ClaimTypes.Name` in your `Login` – Nkosi Oct 08 '16 at 15:19

1 Answers1

9

You have ClaimTypes.NameIdentifier in Application_Start but use ClaimTypes.Name in your Login

You need to change one or the other so that they match what is expected.

protected void Application_Start() {
    //...other code omitted for brevity
    AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Name;
}

You should also make sure that you are not duplicating claims as this will break the AntiForgeryToken call in your view.

As mentioned here MVC5 AntiForgeryToken Claims/“Sequence contains more than one element”

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) {

    if (!ModelState.IsValid) {
        return View(model);
    }

    var identity = new  ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.Name, ClaimTypes.Role);
    identity.AddClaim(new Claim(ClaimTypes.Name, model.Email));
    identity.AddClaim(new Claim(ClaimTypes.Role, "Admin"));
    identity.AddClaim(new Claim(ClaimTypes.Sid, "123"));
    AuthenticationManager.SignIn(new AuthenticationProperties {
        IsPersistent = model.RememberMe
    }, identity);
    return RedirectToLocal(returnUrl);
}
Community
  • 1
  • 1
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • i do that . but show me this error `Sequence contains more than one matching element` – Mr Coder Oct 08 '16 at 16:20
  • Because you are adding `ClaimTypes.Name` more than once. Once in the constructor and again in the following line – Nkosi Oct 08 '16 at 17:06
  • Constructor: `var identity =new ClaimsIdentity(new[] { **new Claim(ClaimTypes.Name, model.Email)** }` and again in next line `identity.AddClaim(**new Claim(ClaimTypes.Name, model.Email**))` – Nkosi Oct 08 '16 at 17:12
  • i comment `identity.AddClaim(**new Claim(ClaimTypes.Name, model.Email**))` bu till show me that error – Mr Coder Oct 08 '16 at 17:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125241/discussion-between-kianoush-and-nkosi). – Mr Coder Oct 08 '16 at 17:19