提供的ClaimsIdentity上没有类型为'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier'的声明 [英] A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' was not present on the provided ClaimsIdentity

查看:206
本文介绍了提供的ClaimsIdentity上没有类型为'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier'的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i在登录中使用Claim.但这告诉我这个错误

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

提供的ClaimsIdentity中没有类型为 http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier 的声明.

如何解决呢?

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

更新

推荐答案

您在 Application_Start 中具有 ClaimTypes.NameIdentifier ,但使用的是 ClaimTypes.Name 在您的登录

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

您还应该确保您没有重复声明,因为这会破坏视图中的 AntiForgeryToken 调用.

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

如此处所述 MVC5 AntiForgeryToken Claims/序列包含更多多于一个元素"

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

这篇关于提供的ClaimsIdentity上没有类型为'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier'的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆