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

时间:2014-07-22 15:58:08

标签: asp.net-mvc owin

我正在使用Owin和Identity,我遇到了声明问题。

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

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

  2. To" obfuscate"用户身份我在显示包含用户信息的页面时使用GUID,每个用户都是唯一的。

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

  3. 当我签署用户时,我有以下声明类型:

    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())
    

    所以我的解释是:

    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.
    

    奇怪的是,用户名没有声明类型。在哪里放置它?

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

    我的逻辑声明类型有问题吗?

2 个答案:

答案 0 :(得分:30)

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

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

ASP.NET Identity使用ClaimTypes.Name存储用户名,使用ClaimTypes.NameIdentifier存储用户的主键GUID。

答案 1 :(得分:8)

如果您查看Facebook或Google从oAuth返回的内容,您会看到ClaimTypes.NameClaimTypes.GivenName + ClaimTypes.Surname。然后LinkedIn返回连接,我相信这是一个错误,因为我有一个完全不同的用户名。 Twitter返回ClaimTypes.Name的用户名,但Twitter是一个特例,他们甚至不回复电子邮件。

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

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