asp.net-core - Jwt 声明名称

标签 asp.net-core asp.net-identity claims-based-identity

在 ASPNetCore 中命名声明的基于标准的方法是什么?

例如,我正在使用以下内容创建 JWT,但我想从中取出字符串。

var claims = new[]
                                {
                    new Claim("UserName", user.UserName),
                    new Claim("UserId", user.Id),
                    new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                    new Claim("AppUserId", appUser.Id.ToString()),
                    new Claim("TenantId", user.TenantId.ToString())
                    };

为此,似乎有两个不同的 dotnetcore 类:
 `System.IdentityModel.Tokens.Jwt.JwtRegisteredClaimNames`
 `System.Security.Claims.ClaimTypes`

我应该使用哪些?

JwtRegisteredClaimNames 如下所示:public const string Sub = "sub";而 ClaimTypes 看起来像这样: public const string Name = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name ";

我不知道什么时候不同。为什么在第二种情况下使用 url?

最佳答案

通常,Dotnet framework 和 Dotnetcore 内部的身份表示都使用 System.Security.Claims.ClaimTypes枚举。声明类型字符串来自 WS-* 时代和 Windows Identity Foundation。我认为它不会因为向后兼容性而改变。System.IdentityModel.Tokens.Jwt.JwtRegisteredClaimNames表示来自较新协议(protocol)的声明类型。

  • https://www.rfc-editor.org/rfc/rfc7519#section-4
  • http://openid.net/specs/openid-connect-core-1_0.html#IDToken

  • 因此,如果您使用较新的协议(protocol)(OAuth、OpenId Connect),您可以使用 JwtRegisteredClaimNames也是。有时您可能需要在两种声明类型之间进行映射。
    另外,sub通常是唯一标识符,对于没有唯一属性(如您可以使用的名称)FamilyNameGivenName .
    您也可以定义自己的声明类型。

    关于asp.net-core - Jwt 声明名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50012155/

    相关文章:

    docker - 如何让 SSL 与 .net core 和 Google Compute Engine 一起工作?

    authentication - 在asp.net core 3.1中注册基于租户的认证方案

    c# - 我如何验证第 3 方用户访问我的 Web API asp.net?

    c# - 多用户类型身份——DbContext设计

    entity-framework - System.NotSupportedException : Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'

    asp.net - Asp.Net Identity 中带有破折号的身份电子邮件

    asp.net - Asp Core身份,登录后如何获取userId?

    c# - 我可以在 ASP.NET Core 和 EF Core 中安全地使用非泛型 DbContextOptions 吗?

    c# - 如何在 ASP .NET CORE Identity 中通过 SignInManager 登录后获取用户声明?

    .net - WIF Saml2SecurityTokenHandler 可以验证顶级签名吗?