首页 文章

脚手架上的MVC Anti伪造令牌错误

提问于
浏览
3

我收到以下错误:

{“类型声明'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier'或'http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/提供的ClaimsIdentity上没有identityprovider' . 要使用基于声明的身份验证启用防伪令牌支持,请验证配置的声明提供程序是否在它生成的ClaimsIdentity实例上提供这两个声明 . 如果配置的声明提供程序使用将不同的声明类型作为唯一标识符,可以通过设置静态属性AntiForgeryConfig.UniqueClaimTypeIdentifier来配置它 . “}

我试过Anti-forgery token issue (MVC 5)没有成功 .

发生错误

@Html.AntiForgeryToken()

Generic Startup.cs

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        AuthConfig.ConfigureAuth(app);
    }
}

Admin Controller Login Method

[HttpPost]
public ActionResult Login(Models.AdminUserLogin LoginModel)
{
    if (ModelState.IsValid)
    {
        if (isUserValid(LoginModel.EmailAddr, LoginModel.Password))
        {
            List<Claim> claims = new List<Claim>
            {
                new Claim(ClaimTypes.Email, LoginModel.EmailAddr),
               //some other claims
            };

            ClaimsIdentity identity = new ClaimsIdentity(claims, AuthConfig.DefaultAuthType);
            IAuthenticationManager authManager = Request.GetOwinContext().Authentication;
            authManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);

            return RedirectToAction("Manage");
        }
        else
        {
            ModelState.AddModelError("", "Username and/or password incorrect");
        }
    }
    return View(LoginModel);
}

任何想法都将非常感激 .

1 回答

  • 4

    在你的 ClaimsIdentity 中,你需要这两个声明来防止伪造令牌:

    List<Claim> claims = new List<Claim>
    {
        // adding following 2 claim just for supporting default antiforgery provider
        new Claim(ClaimTypes.NameIdentifier, LoginModel.EmailAddr),
        new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),
    
        // your other claimes
        new Claim(ClaimTypes.Email, LoginModel.EmailAddr),
        //some other claims
    };
    

相关问题