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

时间:2015-08-20 06:19:10

标签: c# asp.net-mvc asp.net-identity owin claims-based-identity

我收到了以下错误:

  

{"类型&{39; http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier'或者' http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider'未提供的ClaimsIdentity。要使用基于声明的身份验证启用防伪令牌支持,请验证配置的声明提供程序是否在其生成的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);
    }
}

管理员控制器登录方式

[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 个答案:

答案 0 :(得分: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
};