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

防伪令牌问题

华瀚漠
2023-03-14

我对防伪令牌有一个问题:(我创建了自己的用户类,该类工作正常,但现在每当我转到/Account/Register页面时,我都会收到一个错误。错误是:

http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier或http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider类型的索赔在提供的索赔身份中不存在。若要通过基于声明的身份验证启用反伪造令牌支持,请验证配置的声明提供程序是否在其生成的ClaimsId相实例上提供了这两种声明。如果配置的声明提供程序使用不同的声明类型作为唯一标识符,则可以通过设置静态属性AntiForgeryConfig来配置它。UniqueClaimType标识符。

我发现这篇文章:

http://stack247.wordpress.com/2013/02/22/antiforgerytoken-a-claim-of-type-nameidentifier-or-identityprovider-was-not-present-on-provided-claimsidentity/

因此,我将我的应用程序启动方法更改为:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Email;
}

但是当我这样做的时候,我得到了这个错误:

类型的索赔http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress“在所提供的索赔中不存在。

以前有人见过这个吗?如果是的话,你知道怎么解决吗?

public class Profile : User, IProfile
{
    public Profile()
        : base()
    {
        this.LastLoginDate = DateTime.UtcNow;
        this.DateCreated = DateTime.UtcNow;
    }

    public Profile(string userName)
        : base(userName)
    {
        this.CreatedBy = this.Id;

        this.LastLoginDate = DateTime.UtcNow;
        this.DateCreated = DateTime.UtcNow;

        this.IsApproved = true;
    }
    
    [NotMapped]
    public HttpPostedFileBase File { get; set; }

    [Required]
    public string CompanyId { get; set; }

    [Required]
    public string CreatedBy { get; set; }
    public string ModifiedBy { get; set; }

    public DateTime DateCreated { get; set; }
    public DateTime? DateModified { get; set; }
    public DateTime LastLoginDate { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredTitle")]
    public string Title { get; set; }
    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredFirstName")]
    public string Forename { get; set; }
    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredLastName")]
    public string Surname { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredEmail")]
    public string Email { get; set; }
    public string JobTitle { get; set; }
    public string Telephone { get; set; }
    public string Mobile { get; set; }
    public string Photo { get; set; }
    public string LinkedIn { get; set; }
    public string Twitter { get; set; }
    public string Facebook { get; set; }
    public string Google { get; set; }
    public string Bio { get; set; }

    public string CompanyName { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredCredentialId")]
    public string CredentialId { get; set; }
    [Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredSecurityCode")]
    public bool IsLockedOut { get; set; }
    public bool IsApproved { get; set; }

    [Display(Name = "Can only edit own assets")]
    public bool CanEditOwn { get; set; }
    [Display(Name = "Can edit assets")]
    public bool CanEdit { get; set; }
    [Display(Name = "Can download assets")]
    public bool CanDownload { get; set; }
    [Display(Name = "Require approval to upload assets")]
    public bool RequiresApproval { get; set; }
    [Display(Name = "Can approve assets")]
    public bool CanApprove { get; set; }
    [Display(Name = "Can synchronise assets")]
    public bool CanSync { get; set; }

    public bool AgreedTerms { get; set; }
    public bool Deleted { get; set; }
}

public class ProfileContext : IdentityStoreContext
{
    public ProfileContext(DbContext db)
        : base(db)
    {
        this.Users = new UserStore<Profile>(this.DbContext);
    }
}

public class ProfileDbContext : IdentityDbContext<Profile, UserClaim, UserSecret, UserLogin, Role, UserRole>
{
}

I配置文件对于我的存储库来说很简单,如下所示:

public interface IProfile
{
    string Id { get; set; }
    string CompanyId { get; set; }
    
    string UserName { get; set; }
    string Email { get; set; }

    string CredentialId { get; set; }
}

用户类是Microsoft。AspNet。身份实体框架。用户类。我的AccountController如下所示:

[Authorize]
public class AccountController : Controller
{
    public IdentityStoreManager IdentityStore { get; private set; }
    public IdentityAuthenticationManager AuthenticationManager { get; private set; }
    
    public AccountController() 
    {
        this.IdentityStore = new IdentityStoreManager(new ProfileContext(new ProfileDbContext()));
        this.AuthenticationManager = new IdentityAuthenticationManager(this.IdentityStore);
    }

    //
    // GET: /Account/Register
    [AllowAnonymous]
    public ActionResult Register()
    {
        return View();
    }

    //
    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                // Create a profile, password, and link the local login before signing in the user
                var companyId = Guid.NewGuid().ToString();
                var user = new Profile(model.UserName)
                {
                    CompanyId = companyId,
                    Title = model.Title,
                    Forename = model.Forename,
                    Surname = model.Surname,
                    Email = model.Email,
                    CompanyName = model.CompanyName,
                    CredentialId = model.CredentialId
                };

                if (await IdentityStore.CreateLocalUser(user, model.Password))
                {
                    //Create our company
                    var company = new Skipstone.Web.Models.Company()
                    {
                        Id = companyId,
                        CreatedBy = user.Id,
                        ModifiedBy = user.Id,
                        Name = model.CompanyName
                    };

                    using (var service = new CompanyService())
                    {
                        service.Save(company);
                    }

                    await AuthenticationManager.SignIn(HttpContext, user.Id, isPersistent: false);
                    return RedirectToAction("Setup", new { id = companyId });
                }
                else
                {
                    ModelState.AddModelError("", "Failed to register user name: " + model.UserName);
                }
            }
            catch (IdentityException e)
            {
                ModelState.AddModelError("", e.Message);
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

    //
    // POST: /Account/Setup
    public ActionResult Setup(string id)
    {
        var userId = User.Identity.GetUserId();
        using (var service = new CompanyService())
        {
            var company = service.Get(id);
            var profile = new Profile()
            {
                Id = userId,
                CompanyId = id
            };

            service.Setup(profile);

            return View(company);
        }
    }
}

它曾经用[ValidateAntiForgeryToken]属性装饰,但这就是它停止工作的地方。

为什么?

共有3个答案

宋高谊
2023-03-14

把这个放进global.asax.cs

AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimsIdentity.DefaultNameClaimType;
江宏伟
2023-03-14

你知道你的索赔身份中有什么索赔吗?如果没有:

  1. 删除[ValidateAntiForgeryToken]属性
劳华灿
2023-03-14

尝试设置(在global.cs中):

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

  • 我收到以下错误: {“类型的声明”http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier“或者”http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider“在所提供的索赔中不存在。要使用基于声明的身份验证启用防伪令

  • 我有两个web应用程序项目,都位于TFS源代码管理中。第一个项目不会引起反伪造令牌的问题。 这就是错误所在 类型System的异常。发生在System.网页。WebPages.dll但未在用户代码中处理 附加信息:类型为'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier“或者”http://schemas.mi

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

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

  • 场景是:您有一个有效期较长的刷新令牌和一个有效期限较短的访问令牌。 设置:有一个客户端、应用程序服务器和身份验证服务器。 客户端存储访问令牌。 应用程序服务器存储刷新令牌。 身份验证服务器分发刷新访问令牌。 其中一个优点是被盗的访问令牌只能在其有效的时间内使用。 假设黑客窃取了有效期为30分钟的访问令牌。当黑客在30分钟后用有效但过期的被盗访问令牌发出请求时,应用服务器用刷新令牌刷新它,从而黑客获