在Owin Identity和Asp.Net MVC中正确使用声明类型

Mig*_*ura 22 asp.net-mvc owin

我正在使用Owin和Identity,我遇到了索赔问题.

我有应用程序,用户使用电子邮件进行身份验证,其他人使用用户名.

  1. 业务层中的登录方法可以接受电子邮件或用户名,具体取决于具体情况.

  2. 为了"混淆"用户身份,我在显示包含用户信息的页面时使用每个用户唯一的GUID.

    我也使用这个,因为有时电子邮件或用户名可能是网址中的问题...

当我签署用户时,我有以下索赔类型:

new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Name, user.FullName),
new Claim(ClaimTypes.GivenName, user.FirstName),
new Claim(ClaimTypes.Surname, user.LastName),
new Claim(ClaimTypes.NameIdentifier, user.UserUniqueIdentifier.ToString())
Run Code Online (Sandbox Code Playgroud)

所以我的解释是:

Email is the user's email

Name is the user's full name

GivenName is the user's first name

Surname is the user's last name

NameIdentifier is the user's unique identifier ... It can be the email, the username or in this case I am using an Unique ID.
Run Code Online (Sandbox Code Playgroud)

奇怪的是用户名没有索赔类型.它会放在哪里?

基本上,当用户名不用作唯一名称标识符时,似乎存在问题,但仍然有必要.

我的逻辑索赔类型有问题吗?

Ant*_*Chu 31

ClaimTypes.Name (http:// schemas.xmlsoap.org/ws/2005/05/identity/claims/name)应该用于用户名.

ClaimTypes.NameIdentifier通常用于用户的id.在某些情况下,它可能是用户名.

ASP.NET Identity用于ClaimTypes.Name存储用户名,以及存储用户ClaimTypes.NameIdentifier的主键GUID.

  • 您可以创建自己的声明.他们只是字符串.创建一个名为FullName的字符串常量.新索赔(FullName,"John Brock Smith"), (6认同)

V.B*_*.B. 9

如果你检查什么的Facebook或谷歌从OAuth的回报,你会看到ClaimTypes.NameClaimTypes.GivenName + ClaimTypes.Surname.然后LinkedIn返回连接,我相信这是一个错误,因为我有一个完全不同的用户名.Twitter返回用户名ClaimTypes.Name,但Twitter是一个特例,他们甚至不返回电子邮件.

所有这些都使用了一些不透明的数字标识符ClaimTypes.NameIdentifier.他们用自己的字符串名称,通常开始urn:facebook:link,urn:google:profile等自定义的数据.

Asp.NET Identity模型使用UserName ClaimTypes.Name.底线是ClaimTypes.Name在实践中使用的不同.您可以将任何声明名称添加为字符串,并可以添加该urn:...方案以使其明确无误.