asp.net-core - IdentityServer4 - 如何从另一个 ApiResource 调用一个 ApiResource

标签 asp.net-core oauth-2.0 access-token identityserver4 openid-connect

我有一个 Spa 应用程序设置,该应用程序使用 OpenId Connect 的混合授权流程通过 IdentityServer4 进行保护。

此 spa 应用程序能够以登录用户身份通过​​请求 access_token 来调用多个 ApiResources

HttpContextAccessor.HttpContext.GetTokenAsync("access_token")

这对于多个 ApiResources 非常有效。例如,我的 Spa 应用程序可以毫无问题地调用 ApiResource1、ApiResource2 和 ApiResource3。

但是,我添加了一个新的 ApiResource(即 ApiResource4),并且我需要能够从 ApiResource1 调用它。但是,当我尝试调用

HttpContextAccessor.HttpContext.GetTokenAsync("access_token")

来自 ApiResource1 的 access_token 为 null,因此调用失败。

如何使用登录用户的 token 从 ApiResource1 调用 ApiResource2?

也许更好的提问方式是,如何从 ApiResource1 获取登录用户的 access_token?

我的 SPA 应用程序设置如下:

services.AddAuthentication(options =>
                {
                    options.DefaultScheme = "Cookies";
                    options.DefaultChallengeScheme = "oidc";
                })
                .AddCookie("Cookies", options =>
                {
                    options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
                })
                .AddOpenIdConnect("oidc", options =>
                {
                    options.SignInScheme = "Cookies";

                    options.Authority = Configuration.GetSection("IdentityServerOptions").GetValue<string>("BaseUrl");
                    options.RequireHttpsMetadata = true;

                    options.ClientId = Configuration.GetSection("IdentityServerOptions").GetValue<string>("ClientId");
                    options.ClientSecret = Configuration.GetSection("IdentityServerOptions").GetValue<string>("ClientSecret");

                    options.SaveTokens = true;
                    options.GetClaimsFromUserInfoEndpoint = true;

                    options.Scope.Add("ApiResource1");
                    options.Scope.Add("ApiResource2");
                    options.Scope.Add("ApiResource3");
                    options.Scope.Add("ApiResource4");
                    options.ResponseType = "code id_token";
                });

我的 ApiResources 设置如下:

services.AddAuthentication("Bearer")
                .AddIdentityServerAuthentication(options =>
                {
                    options.Authority = Configuration.GetSection("IdentityServer").GetValue<string>("BaseUrl");
                    options.RequireHttpsMetadata = true;
                    options.ApiName = _AppName;
                });

最佳答案

HttpContext.GetTokenAsync("access_token") 扩展方法仅适用于 Cookie 身份验证。它将尝试提取存储在 cookie 中的 token 。当您有使用 JWT 身份验证的 API 时,此功能不可用。

您有两个选择。

  1. 使用扩展授权委托(delegate),如 Identity Server 文档 http://docs.identityserver.io/en/release/topics/extension_grants.html#example-simple-delegation-using-an-extension-grant 中所述。 。此选项实现起来比较复杂,但提供了更高的安全性。

  2. Authorization header 中提取并转发 JWT。此选项非常简单,只需在发出 API 请求之前从请求中获取 header 并将其设置在 HttpClient 的 header 上即可。

关于asp.net-core - IdentityServer4 - 如何从另一个 ApiResource 调用一个 ApiResource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51604191/

相关文章:

asp.net-mvc - 托管多个 ASP.NET Core 应用程序

unit-testing - 如何对 Asp.net core Web API (net452) 项目进行单元测试?

c# - Asp.net core 2 - 文件未发布

authentication - keycloak 删除自动添加的默认角色

facebook - 如何使用 Graph API 以编程方式向页面添加事件?

c# - 如何将文件和 json 对象从 postman 传递到 asp.net 核心 webapi

javascript - 为什么 Swagger 规范不提供 OAuth2 身份验证的 clientId?

javascript - 使用 Gmail API 访问用户邮件列表

objective-c - Facebook 连接类与单例 : Access token issue

vb.net - 为什么此代码无法从 oAuth2 stex 中的刷新代码获取刷新代码?