【问题标题】:Pass user claims to downstream (secondary) APIs?将用户声明传递给下游(辅助)API?
【发布时间】:2018-09-10 06:40:50
【问题描述】:

我尝试过搜索,但令人惊讶的是找不到我的问题的答案。

我正在设计一个 Web 应用程序,它将通过 Angular 提供一个前端接口,以及多个下游 API。如下:

 [API - A Client] -> [API - A] -> [API - B]

我正在使用 IdentityServer4 进行身份验证/授权。一些用户会有一个特定的声明,我们称之为“Foo”,并且当通过 SPA 客户端与 API A 交互时,该声明会正确地从身份验证服务器传递到 API A(使用隐式流)。

但是,我无法将该声明从 API A 传递到使用客户端凭据的 API B。根据我的阅读/研究,这似乎是正确的行为,因为它的客户端凭据流。

所以我的问题是,如何将用户声明(“Foo”)传递到下游的第二层 API (API-B)?我需要使用不同的流程吗? API-A 是否应手动将其随请求传递给 API-B?

这是我第一次使用 IdentityServer / OpenID connect / OAuth,我愿意改变。

IdentityServer4 配置

public class Config
{
    public static IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource>
        {
            new ApiResource("API-B", "API B")
            {
                UserClaims = { "Foo" }
            },
            new ApiResource("API-A", "API A")
            {
                ApiSecrets = {new Secret("Secret") },
                UserClaims = { "Foo",  },
            }
        };
    }

    public static IEnumerable<Client> GetClients()
    {
        return new List<Client>
        {
            new Client
            {
                ClientName = "API-A Client",
                ClientId = "API-A_client",
                AllowedGrantTypes = GrantTypes.Implicit,

                RedirectUris = { "http://localhost:7900/swagger/oauth2-redirect.html" },
                PostLogoutRedirectUris = { "http://localhost:7900/" },

                RequireConsent = false,
                AllowAccessTokensViaBrowser = true,

                AllowedScopes = new List<string>(){
                    "API-A",
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile
                }
            },

            new Client
            {
                ClientName = "API-A Backend",
                ClientId = "API-A_backend",
                AllowedGrantTypes = GrantTypes.ClientCredentials,

                ClientSecrets = {new Secret("Secret".Sha256()) },

                AllowedScopes = new List<string>()
                {
                    "API-B",
                    "custom_resource",
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile
                },
                AlwaysIncludeUserClaimsInIdToken = true,
                AlwaysSendClientClaims = true,
            }
        };
    }

    public static IEnumerable<IdentityResource> GetIdentityResources()
    {
        return new List<IdentityResource>
        {
            new IdentityResources.OpenId(),
            new IdentityResources.Profile(),
            new IdentityResource("custom_resource", new [] { "Foo" }),
        };
    }
}

API A 身份验证配置

services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "http://localhost:6900";
                options.ApiName = "API-A";
                options.RequireHttpsMetadata = false; // dev only!
            });
services.AddTransient<AccessTokenDelegatingHandler>((service) => new AccessTokenDelegatingHandler(tokenEndpoint: $"http://localhost:6900/connect/token", clientId: "API-A", clientSecret: "Secret", scope: "API-B"));

        services.AddHttpClient<ApiBHttpClient>(client =>
        {
            client.BaseAddress = new Uri(Configuration["ApiBUri"]);
            client.DefaultRequestHeaders.Add("Accept", "application/json");
        })
        .AddHttpMessageHandler<AccessTokenDelegatingHandler>();

API B 身份验证配置

    services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "http://localhost:6900";
                options.ApiName = "API-B"; // required audience of access tokens
                options.RequireHttpsMetadata = false; // dev only!
                options.ApiSecret = "Secret";
            });

上面的结果是 API-A 正确地通过 IdentityClaims 访问了“Foo”,但是 API-B 没有(尽管调用成功)。

感谢任何帮助!

【问题讨论】:

    标签: asp.net-core-2.0 identityserver4


    【解决方案1】:

    终于找到这个GitHub页面,问同样的问题:https://github.com/IdentityServer/IdentityServer4/issues/1679

    关于延期补助金,http://docs.identityserver.io/en/release/topics/extension_grants.html 这是我的确切情况。

    【讨论】:

    • 链接不再存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 2013-01-16
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 2012-12-28
    • 2016-04-16
    相关资源
    最近更新 更多