c# - IdentityServer4 ValidIssuers

标签 c# asp.net-core identityserver4

有没有办法告诉 IdentityServer4 的身份验证系统允许多个颁发者颁发 token ?

我有一个使用 Identity Server 发布不记名 token 的应用程序,只要前端和后端使用相同的 URL 从身份验证中获取 token 就可以正常工作。

但是,我现在需要通过多个 CNAME 访问同一个站点,这意味着客户端将从两个不同的 URL 请求 token 。

发送到日志的错误是:

info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[7]

Bearer was not authenticated. Failure message: IDX10205: Issuer validation failed. Issuer: 'http://domainb.com'. Did not match: validationParameters.ValidIssuer: 'http://domaina.com' or validationParameters.ValidIssuers: 'null'.



ValidIssuers 集合的存在似乎表明您可以设置 API 接受 token 的多个位置,但我在 UseIdentityServerAuthentication 公开的选项中找不到类似的内容。

我知道权限选项,但这只允许我设置一个有效的权限。

有没有办法设置多个有效的发行者,或者将其设置为使用主机名以外的东西作为发行者 ID?

更新

我在服务器端的身份服务器配置如下所示:

services.AddIdentityServer(options => { 
                             options.IssuerUri = "http://authserver"; })
     .AddAspNetIdentity<ApplicationUser>();

这是来自身份验证服务器方面的事情。

在客户端 API 上,UseIdentityServerAuthentication 调用如下所示:

app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions()
{
    Authority = AppSettingsConfigurationRoot["Authentication:AuthorityEndpoint"],
    RequireHttpsMetadata = false,
    ApiName = "rqapi",
    AutomaticAuthenticate = true,
    ClaimsIssuer = "http://localhost:5001"
});

{{AppSettingsConfigurationROot["Authentication:AuthorityEndpoint"] 中的地址通常设置为服务器的公共(public) DNS 名称,以便 AngularJS 看到的 token 颁发者从 C# API 的角度匹配 IdentityServer 的 URL。

最佳答案

Original Poster wrote in a comment ,(现在,2020 年,已弃用)IdentityServer4.AccessTokenValidation package没有公开正确的选择。阅读有关最近弃用 check this blogpost 的更多信息,但是如果您仍在使用它,这就是我解决此问题的方法。
The AddIdentityServerAuthentication(...) extension method是一个包装器(代码是 super 可读的!)结合两种身份验证方案:

  • JwtBearer
  • OAuth2内省(introspection)

  • 它使用自己的配置类,并且根本不公开所有 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
            };
        });
    
    据我了解,这将使用 example.org作为权威并从该域中获取 openid-configuration 等等。但是,只要 ValidIssuers 之一,提供给此 API 的任何 JWT token 都将被接受。是 iss (发行人声明)在 token 中。

    关于c# - IdentityServer4 ValidIssuers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49950579/

    相关文章:

    c# - 调用 System.IO.StreamReader 时转义字符格式错误

    identityserver4 - 在 IdentityServer4 中重用 IdentityServer3 刷新 token

    identityserver4 - 为什么混合流是 IdentityServer 中 Web 应用程序的默认推荐选项?

    c# - 使用 C# 在 Windows Mobile 5.0 中保存图像

    c# - 不断越界异常

    c# - 如何在 Windows 10 MapControl 中将 Z-Index 添加到 XAML 控件

    c# - mscorlib.dll 中发生类型为 'System.StackOverflowException' 的未处理异常

    c# - .Net Core 中的 DbContextTransaction 在哪里

    c# - 异步计时问题

    api - 生成访问 token 并通过 Azure API 管理针对 IdentityServer4 进行验证