gpt4 book ai didi

c# - 脚手架上的 MVC 防伪 token 错误

转载 作者:行者123 更新时间:2023-11-30 15:24:55 26 4
gpt4 key购买 nike

我收到以下错误:

{"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."}

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

发生错误

@Html.AntiForgeryToken()

通用 Startup.cs

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

管理 Controller 登录方法

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

任何想法将不胜感激。

最佳答案

您需要在 ClaimsIdentity 中同时声明这两个声明才能防伪造 token :

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

关于c# - 脚手架上的 MVC 防伪 token 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32110852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com