gpt4 book ai didi

c# - ClaimType 的 ASP.NET 要求

转载 作者:行者123 更新时间:2023-12-02 20:11:28 35 4
gpt4 key购买 nike

我正在研究在 ASP.NET (MVC Core 1.0) 中使用基于声明的授权。设置 ClaimsIdentity 时,我提供了一个键/值字符串对列表来表示每个 Claim。示例:

List<Claim> claims = new List<Claim>
{
new Claim("UserID", user.ID),
new Claim("Name", user.Name),
new Claim("Role", "basic")
};

我的理解是我可以使用我想要的任何键/值。但我注意到有一些预定义的键可以通过 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")
};

问题:

  1. 如果我使用预定义的键,是否有关于分配给每个键的实际值的任何规则/限制,或者它是应用程序定义的?例如,是否可以将数据库主键粘贴到 ClaimTypes.Sid​​ 中,或者 ASP.NET 对 ClaimTypes.Sid​​ 应包含的内容是否有某些期望?

  2. 是否有任何必需ClaimTypes,或者完全由应用程序决定包含或不包含哪些内容?我想答案可能取决于我将与之交互的特定第三方身份验证服务,但是不使用任何第三方身份验证的独立 ASP.NET 项目的简单情况又如何呢? ASP.NET本身有什么要求吗?

任何有关使用特定键/值的要求和/或最佳实践的链接将不胜感激。

最佳答案

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?

如果您生成的 Claim,则使用预定义的 ClaimTypes 之一也会修改 Type 属性。 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(而非第三方来源)中,您的空间会受到一定限制; cookies cannot be larger than 4096 bytes in total .

到目前为止,我发现的有关 ASP.NET Core 声明身份验证的最佳文章是 herehere 。截至本文发布时,我们仍处于 RC1 阶段,因此在最终发布之前某些细节可能会发生变化。

关于c# - ClaimType 的 ASP.NET 要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36179662/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com