ASP.NET 对 ClaimType 的要求
ASP.NET requirements for ClaimTypes
我正在研究在 ASP.NET (MVC Core 1.0) 中使用基于声明的授权。在设置 ClaimsIdentity
时,我提供了一个 key/value 字符串对列表来表示每个 Claim
。示例:
List<Claim> claims = new List<Claim>
{
new Claim("UserID", user.ID),
new Claim("Name", user.Name),
new Claim("Role", "basic")
};
我的理解是我可以使用任何我想要的keys/values。但我注意到通过 ClaimsType
class 可以使用一些预定义的键。因此,我可能会改用其中一些预定义键:
List<Claim> claims = new List<Claim>
{
new Claim(ClaimTypes.Sid, user.ID),
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.Role, "basic")
};
问题:
如果我使用预定义的键,是否有关于分配给每个键的实际值的任何rules/restrictions,或者它是由应用程序定义的吗?例如,在 ClaimTypes.Sid
中粘贴数据库主键是否可以,或者 ASP.NET 是否对 ClaimTypes.Sid
应该包含的内容有一定的期望?
是否有任何 ClaimTypes
是 必需的 ,或者完全由应用程序决定包含或不包含的内容?我想答案可能取决于我将与之交互的特定第三方身份验证服务,但是不使用任何第三方身份验证的独立 ASP.NET 项目的简单情况如何。 ASP.NET本身有什么要求吗?
任何指向关于使用特定 key/value 的要求 and/or 最佳实践的链接将不胜感激。
If I use the pre-defined keys, are there any rules/restrictions
regarding the actual values assigned to each key, or is it application
defined? For example, is it OK to stick a database primary key in
ClaimTypes.Sid, or does ASP.NET have certain expectations of what
ClaimTypes.Sid should contain?
使用预定义的 ClaimTypes
之一也将修改类型 属性 如果您的结果 Claim
。 You can find a list of these types here。据我所知,您可以随意将数据库 ID 放入 ClaimTypes.Sid
,但是我强烈建议您使用自己的名字来称呼它。
Are there any ClaimTypes that are required, or is it completely up to
the application to decide what to include or not include? I imagine
the answer may depend on specific third-party authentication services
I would interact with, but how about the simple case of a
self-contained ASP.NET project that does not use any third-party
authentication. Does ASP.NET itself have any requirements?
假设没有第三方,您可以决定什么是必需的,什么不是必需的。请记住,如果您将声明存储在 cookie 中(而非第三方来源),您的 space 会受到一定程度的限制; cookies cannot be larger than 4096 bytes in total.
到目前为止,我找到的关于 ASP.NET 核心声明身份验证的最佳文章是 here and here。截至本文发布时,我们仍处于 RC1 版本,因此在最终版本发布之前,一些细节可能会发生变化。
- If I use the pre-defined keys, are there any rules/restrictions regarding the actual values assigned to each key, or is it application defined? For example, is it OK to stick a database primary key in ClaimTypes.Sid, or does ASP.NET have certain expectations of what ClaimTypes.Sid should contain?
基本没有规则限制,但要看token的消费情况。 Asp.Net 默认情况下,身份要求用户名为 ClaimTypes.Name
(用户显示名称或邮件,无论您使用什么),角色为 ClaimTypes.Role
和用户 ID(不需要行 ID,只需唯一标识用户,即 Guid 或电子邮件地址)作为 ClaimTypes.NameIdentifier
。默认也可以看到here on GitHub.
也就是说,如果您使用自定义声明类型,则需要在配置 Asp.Net 身份时在 ClaimsIdentityOptions
中说明。
UserNameClaimType
中设置的声明类型是当您User.Identity.Name
在您的控制器中访问它时使用的声明类型。如果您的声明类型与 ClaimsIdentityOptions
中的不匹配,它将只是 return null。
我正在研究在 ASP.NET (MVC Core 1.0) 中使用基于声明的授权。在设置 ClaimsIdentity
时,我提供了一个 key/value 字符串对列表来表示每个 Claim
。示例:
List<Claim> claims = new List<Claim>
{
new Claim("UserID", user.ID),
new Claim("Name", user.Name),
new Claim("Role", "basic")
};
我的理解是我可以使用任何我想要的keys/values。但我注意到通过 ClaimsType
class 可以使用一些预定义的键。因此,我可能会改用其中一些预定义键:
List<Claim> claims = new List<Claim>
{
new Claim(ClaimTypes.Sid, user.ID),
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.Role, "basic")
};
问题:
如果我使用预定义的键,是否有关于分配给每个键的实际值的任何rules/restrictions,或者它是由应用程序定义的吗?例如,在
ClaimTypes.Sid
中粘贴数据库主键是否可以,或者 ASP.NET 是否对ClaimTypes.Sid
应该包含的内容有一定的期望?是否有任何
ClaimTypes
是 必需的 ,或者完全由应用程序决定包含或不包含的内容?我想答案可能取决于我将与之交互的特定第三方身份验证服务,但是不使用任何第三方身份验证的独立 ASP.NET 项目的简单情况如何。 ASP.NET本身有什么要求吗?
任何指向关于使用特定 key/value 的要求 and/or 最佳实践的链接将不胜感激。
If I use the pre-defined keys, are there any rules/restrictions regarding the actual values assigned to each key, or is it application defined? For example, is it OK to stick a database primary key in ClaimTypes.Sid, or does ASP.NET have certain expectations of what ClaimTypes.Sid should contain?
使用预定义的 ClaimTypes
之一也将修改类型 属性 如果您的结果 Claim
。 You can find a list of these types here。据我所知,您可以随意将数据库 ID 放入 ClaimTypes.Sid
,但是我强烈建议您使用自己的名字来称呼它。
Are there any ClaimTypes that are required, or is it completely up to the application to decide what to include or not include? I imagine the answer may depend on specific third-party authentication services I would interact with, but how about the simple case of a self-contained ASP.NET project that does not use any third-party authentication. Does ASP.NET itself have any requirements?
假设没有第三方,您可以决定什么是必需的,什么不是必需的。请记住,如果您将声明存储在 cookie 中(而非第三方来源),您的 space 会受到一定程度的限制; cookies cannot be larger than 4096 bytes in total.
到目前为止,我找到的关于 ASP.NET 核心声明身份验证的最佳文章是 here and here。截至本文发布时,我们仍处于 RC1 版本,因此在最终版本发布之前,一些细节可能会发生变化。
- If I use the pre-defined keys, are there any rules/restrictions regarding the actual values assigned to each key, or is it application defined? For example, is it OK to stick a database primary key in ClaimTypes.Sid, or does ASP.NET have certain expectations of what ClaimTypes.Sid should contain?
基本没有规则限制,但要看token的消费情况。 Asp.Net 默认情况下,身份要求用户名为 ClaimTypes.Name
(用户显示名称或邮件,无论您使用什么),角色为 ClaimTypes.Role
和用户 ID(不需要行 ID,只需唯一标识用户,即 Guid 或电子邮件地址)作为 ClaimTypes.NameIdentifier
。默认也可以看到here on GitHub.
也就是说,如果您使用自定义声明类型,则需要在配置 Asp.Net 身份时在 ClaimsIdentityOptions
中说明。
UserNameClaimType
中设置的声明类型是当您User.Identity.Name
在您的控制器中访问它时使用的声明类型。如果您的声明类型与 ClaimsIdentityOptions
中的不匹配,它将只是 return null。