Ric*_*ish 6 c# asp.net-core identityserver4
有什么方法可以告诉 IdentityServer4 的身份验证系统允许令牌的多个发行者吗?
我有一个应用程序使用 Identity Server 来发布不记名令牌,只要前端和后端使用相同的 URL 从身份验证中获取令牌就可以正常工作。
但是,我现在需要通过多个 CNAME 访问同一个站点,这意味着客户端将从两个不同的 URL 请求令牌。
发送到日志的错误是:
信息:Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[7]
Bearer 未经过身份验证。失败消息:IDX10205:颁发者验证失败。发行人:“ http://domainb.com ”。不匹配:validationParameters.ValidIssuer:' http : //domaina.com '或validationParameters.ValidIssuers:'null'。
ValidIssuers 集合的存在似乎表明您可以设置 API 将接受令牌的多个位置,但我在 UseIdentityServerAuthentication 公开的选项中找不到任何类似的内容。
我知道权限选项,但这仅允许我设置单个有效权限。
有没有办法设置多个有效的发行者,或者将其设置为使用主机名以外的其他内容作为发行者 ID?
更新
我在服务器端的身份服务器配置如下所示:
services.AddIdentityServer(options => {
options.IssuerUri = "http://authserver"; })
.AddAspNetIdentity<ApplicationUser>();
Run Code Online (Sandbox Code Playgroud)
这是来自身份验证服务器方面的事情。
在客户端 API 上, UseIdentityServerAuthentication 调用如下所示:
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions()
{
Authority = AppSettingsConfigurationRoot["Authentication:AuthorityEndpoint"],
RequireHttpsMetadata = false,
ApiName = "rqapi",
AutomaticAuthenticate = true,
ClaimsIssuer = "http://localhost:5001"
});
Run Code Online (Sandbox Code Playgroud)
{{AppSettingsConfigurationROot["Authentication:AuthorityEndpoint"] 中的地址通常设置在服务器的公共 DNS 名称处,以便从 C# API 的角度来看,AngularJS 看到的令牌颁发者与 IdentityServer 的 URL 匹配。
正如原始海报在评论中所写,(现在,2020 年,已弃用)IdentityServer4.AccessTokenValidation 包没有公开正确的选项。要阅读有关最近弃用的更多信息,请查看此博客文章,但如果您仍在使用它,请参阅我解决此问题的方法。
该AddIdentityServerAuthentication(...)扩展方法是一个包装(代码是超级可读!)来组合2种的身份验证方案:
它使用自己的配置类,并且根本不公开所有JwtBearer选项(可能只是一个遗漏,可能是因为某些选项对两种方案都无效。
如果 - 像我一样 - 你只需要 JwtBearer你可能只使用它,并使用ValidIssuers数组。所以:
services.AddAuthentication("Bearer")
.AddJwtBearer(options =>
{
options.Authority = "https://example.org";
options.Audience = "foo-api"; // options.ApiName in the IDS4 variant
options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuers = new[]
{
"https://example.org", // first issuer
"https://example.com", // some other issuer
},
NameClaimType = "name", // To mimick IDS4's variant
RoleClaimType = "role", // To mimick IDS4's variant
};
});
Run Code Online (Sandbox Code Playgroud)
据我了解,这将example.org用作权限并从该域中获取 openid-configuration 等。但是,提供给该API的任何JWT令牌将被接受,只要一个的ValidIssuers是iss在令牌(发行人要求)。