支架上的MVC反伪造令牌错误 [英] MVC Anti forgery token error on scaffold
问题描述
我收到以下错误:
{类型为'
http://schemas. xmlsoap.org/ws/2005/05/identity/claims/nameidentifier "或"
{"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."}
我尝试了反伪造令牌问题(MVC 5),没有成功.
I have tried Anti-forgery token issue (MVC 5) with no success.
发生错误
@Html.AntiForgeryToken()
通用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);
}
任何想法都将不胜感激.
Any ideas would be very appreciated.
推荐答案
您需要在ClaimsIdentity
中同时拥有这两个声明,才能防止伪造令牌的工作:
You need both this two claims in your ClaimsIdentity
to anti forgery token works:
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
};
这篇关于支架上的MVC反伪造令牌错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!