gpt4 book ai didi

c# - 提供的 ClaimsIdentity 上不存在类型为 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' 的声明

转载 作者:太空宇宙 更新时间:2023-11-03 18:24:11 24 4
gpt4 key购买 nike

我在登录时使用声明。但它告诉我这个错误

A claim of type http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier was not present on the provided ClaimsIdentity.

如何解决这个问题?

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

更新

enter image description here

最佳答案

您在 Application_Start 中有 ClaimTypes.NameIdentifier,但在您的 Login 中使用 ClaimTypes.Name

您需要更改一个或另一个以使它们符合预期。

protected void Application_Start() {
//...other code omitted for brevity
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Name;
}

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

如此处所述MVC5 AntiForgeryToken Claims/“Sequence contains more than one element”

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

关于c# - 提供的 ClaimsIdentity 上不存在类型为 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' 的声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39931504/

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