UniqueClaimTypeIdentifier 到底发生了什么?

UniqueClaimTypeIdentifier what exactly is happening?

我正在执行基于声明的用户角色身份验证。对于此身份验证,我测试了以下内容:

var identity = new ClaimsIdentity(new[] {
                new Claim(ClaimTypes.Name, name),
                new Claim(ClaimTypes.Email, email),
                new Claim(ClaimTypes.StreetAddress, Address),
                new Claim(ClaimTypes.Role, "Admin")
},

我的身份验证工作正常,但我意识到我应该实施某种安全措施以避免用户篡改设置的角色。

因此我偶然发现了这个,应该在 Global.asax 中添加:

AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Email;

我似乎不能完全理解上面这段代码的作用?我说得对吗,如果我说它给了用户持有的 cookie,一个由 email 制成的唯一令牌,然后被系统用来验证用户的合法性?

顺便说一句,我是新手,所以对我放轻松 :-)

我不太确定你的问题是什么意思,但让我试着澄清一些事情。

首先,让我们谈谈您的 AntiForgeryConfig 行代码。它所做的是将 AntiForgeryToken 配置为使用 Email 声明来识别请求(根据电子邮件创建令牌)。 AntiForgeryToken 允许您信任请求并防止跨站点请求伪造 (CSRF)。

分两部分实现。首先,您需要将 AntiForgeryToken 添加到表单 (@Html.AntiForgeryToken) 中。其次,您需要在控制器的操作中验证令牌 (ValidateAntiForgeryTokenAttribute)。

Here is a link to explain what CSRF

Here is a link with up to date code how to implement it

作为旁注,您说 ... to avoid that the user can tamper the set role。 AntiForgeryToken 对篡改角色没有任何作用。篡改角色将与您的身份验证过程更相关。