当前位置: 首页 > 知识库问答 >
问题:

脚手架上的MVC防伪令牌错误

钱均
2023-03-14

我收到以下错误:

{“类型的声明”http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier“或者”http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider“在所提供的索赔中不存在。要使用基于声明的身份验证启用防伪令牌支持,请验证配置的声明提供程序是否在其生成的ClaimSideEntity实例上提供了这两个声明。如果配置的声明提供程序使用不同的声明类型作为唯一标识符,则可以通过设置静态属性AntiForgeryConfig对其进行配置。UniqueClaimTypeIdentifier。"}

我尝试过防伪代币发行(MVC 5),但没有成功。

错误发生在

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

任何想法都将不胜感激。

共有1个答案

郤仰岳
2023-03-14

你需要这两个声明在你的ClaimsId相反伪造令牌工作:

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
};
 类似资料:
  • 我对防伪令牌有一个问题:(我创建了自己的用户类,该类工作正常,但现在每当我转到/Account/Register页面时,我都会收到一个错误。错误是: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier或http://schemas.microsoft.com/accesscontrolservice/2010/0

  • 问题内容: 我对MVC项目的以下详细信息有疑问。 当我尝试将jquery ajax请求与加载面板(例如旋转gif(甚至文本))一起使用时,从提琴手观察到我收到错误消息 所需的反伪造表单字段“ __RequestVerificationToken”不存在。 如果我 在POST动作方法上发表评论 并使用加载面板,它工作正常,我想知道为什么会收到此错误。 我什至使用了查询字符串 还是我出错了 防伪令牌无

  • 问题内容: 我将JQuery与ASP.NET Core 1.0.1一起使用,并且我有Ajax调用: 对ASP.NET Core的操作: 该表单处于共享视图中,如下所示: 提交表单时出现错误: 如何通过JQuery Ajax调用启用ASP.NET Core的AntiForgeryToken? 更新 我需要在表单中添加以下asp-controller和asp-action: 这将生成防伪令牌。我需要手

  • 前言 相信很多人都用过vue-cli或create-react-app或者类似的脚手架。 脚手架方便我们复制,粘贴,或者clone代码库,而且还可以更具用户的选择,引入用户所需要的插件。 脚手架往往搭配着早已设计好了架构的项目,然后按需进行拷贝。 Yeoman 介绍 官网介绍: The web's scaffolding tool for modern webapps. yeoman是一款来做脚手

  • 脚手架工具 脚手架是指为整个应用生成一个初始化的模板, 而不是生成访问数据库的代码. Yeoman Slush

  • 问题内容: 我想为基于Struts 1.x框架的Web应用程序实施跨站点请求伪造预防。我知道struts 2框架为此提供了令牌拦截器,并且可以使用过滤器实现类似的功能。 我对一些想法感到困惑1)如何以简单的方式生成唯一令牌?(我可以为此目的使用Action类令牌来避免重复提交表单) 将struts 1.x框架令牌机制用于CSRF预防是否存在任何问题 问题答案: Struts 1 Action令牌方