我创建了一个需要访问多个 API 的 asp.net 核心 Web 应用程序。身份验证应该通过 ADFS 进行。我能够设置 OpenId Connect 身份验证并检索单个 api 资源的 access_token。如何检索其余 API 的 access_tokens,以便我可以调用它们或一个具有多个受众的 access_token?
通常我应该添加范围,但似乎 ADFS 对于范围的含义与 IdentityServer 不同,而是使用“资源”选项。
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "oidc";
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = appSettings.Authority;
options.ClientId = appSettings.ClientId;
options.ClientSecret = appSettings.ClientSecret;
options.SaveTokens = true;
foreach (var scope in appSettings.Scopes)
{
options.Scope.Add(scope);
}
options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
options.RequireHttpsMetadata = true;
options.Resource = appSettings.TemplateServiceIdentifier;
});
然后我可以,在需要的时候打电话
var token = await _httpContextAccessor.HttpContext.GetTokenAsync(OpenIdConnectParameterNames.AccessToken);
然后将令牌添加为承载并验证它就好了。如果我从一开始就添加它,这对每一个资源 (API) 都适用。但是,我似乎一次无法获得多个资源的访问令牌。
ADFS 甚至可以吗?我是否应该为具有多个受众的所有 API 资源检索一个访问令牌(因为所有 API 的身份提供者都相同)?