c# - 如何仅通过 userinfo 端点而不是访问 token 获取声明

标签 c# authentication asp.net-core openid identityserver4

我想通过 userinfo 端点获取自定义声明。这也有效,但所有自定义声明也都在访问 token 中。我是这样理解的,我可以在访问 token 中放置一两个声明(例如电子邮件或名称),而所有其他声明(given_name,...)都可以通过 userinfo 端点访问。 (并且它们不在访问 token 中)

我的 ProfileService 看起来像这样:

public class ProfileService : IProfileService
    {
        private readonly IUserClaimsPrincipalFactory<CustomUser> _claimsFactory;
        private readonly UserManager<CustomUser> _userManager;

        public ProfileService(UserManager<CustomUser> userManager,    IUserClaimsPrincipalFactory<CustomUser> claimsFactory)
        {
            _claimsFactory = claimsFactory;
            _userManager = userManager;
        }

        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            var sub = context.Subject.GetSubjectId();
            var user = await _userManager.FindByIdAsync(sub);
            var principal = await _claimsFactory.CreateAsync(user);

            var claims = principal.Claims.ToList();
            claims = claims.Where(claim => context.RequestedClaimTypes.Contains(claim.Type)).ToList();

            claims.Add(new Claim(JwtClaimTypes.GivenName, user.FirstName));

            context.IssuedClaims = claims;

        }

        public async Task IsActiveAsync(IsActiveContext context)
        {
            var sub = context.Subject.GetSubjectId();
            var user = await _userManager.FindByIdAsync(sub);
            context.IsActive = user != null;
        }
    }

这是我的 config.cs 文件:
return new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResources.Email(),
            };
        }

 public static IEnumerable<ApiResource> GetApiResources()
      {
          return new List<ApiResource>
          {
              new ApiResource("api1", "My API", new [] {JwtClaimTypes.Name })
          };
      }
new Client
                {
                    ClientId = "xxx",
                    ClientName = "xxx",
                    //AccessTokenType = AccessTokenType.Reference,
                    AccessTokenType = AccessTokenType.Jwt,
                    AllowedGrantTypes = GrantTypes.Implicit,
                    AllowAccessTokensViaBrowser = true,
                    RequireConsent = false,

                    RedirectUris =           { "http://localhost:4200/home" },
                    PostLogoutRedirectUris = { "http://localhost:4200/unauthorized" },
                    AllowedCorsOrigins =     { "http://localhost:4200" },

                    AllowedScopes =
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        IdentityServerConstants.StandardScopes.Email,
                        "api1"
                       }
                }
            };

我误解了吗?

最佳答案

GetProfileDataAsync被调用两次,但上下文不同。

  • 对于访问 token Context.Caller = ClaimsProviderAccessToken。
  • 对于身份 token Context.Caller = 用户信息端点。

  • 请注意,对于这两种情况,所请求的声明是不同的。

    如果您只想为身份添加声明,您可以通过将这些声明添加到过滤器 (IdentityResource) 来配置 identityserver 以包含这些声明,在这种情况下,您无需在 GetProfileDataAsync 中添加其他声明。根本。或者,如果您想添加特定声明,请检查当前上下文。

    所以在 GetProfileDataAsync你可以有如下内容:
    if (Context.Caller == "UserInfoEndpoint")
        claims.Add(new Claim(JwtClaimTypes.GivenName, user.FirstName));
    

    这应该仅将声明添加到 userinfo。

    关于c# - 如何仅通过 userinfo 端点而不是访问 token 获取声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47816168/

    相关文章:

    c# - ReSharper 中可能的空赋值。我看不出那怎么可能为空?

    python - 使用 mod-python 使 HTTP 身份验证成为可选

    java - tomcat 7重启后恢复用户登录

    c# - asp.net 核心默认代理

    c# - 使用 docker-compose 使用 dotnet 核心的 MongoDB 连接超时

    c# - .NET 中集合的内存分配

    c# - 从 ActionFilterAttribute 返回带有模型的 View

    javascript - 我们是否需要 NodeJs 来在服务器上运行 react 应用程序?

    c# - 为什么属性(property)访问比成员(member)访问慢

    c# - 同步框架 - 跨域文件同步