docker - 在 Linux 和 Azure 存储库上运行的 Docker 镜像中的 IdentityServer4 和身份验证问题

标签 docker asp.net-core jwt identityserver4

我的解决方案由三个项目组成,它们是:

  1. 托管 IdentityServer 的 ASP.NET MVC Core 项目。
  2. 一个 protected 并管理 IdentityServer 的 ASP.NET Core API 项目。
  3. 另一个调用 API 的 ASP.NET MVC Core。

因此,MVC 客户端还必须针对每个请求向 API 发送 access_token。 如果我使用 docker-compose 命令运行该解决方案,它可以工作,但如果我将图像推送到 Azure 存储库或从 Azure 存储库拉取图像,则会遇到问题。

我得到的错误是:ErrorMessage:Bearer error =“invalid_token”,error_description =“找不到签名 key ”

这是我的配置

services.AddIdentityServer()
    .AddDeveloperSigningCredential()
    .AddAspNetIdentity<ApplicationUser>()
    .AddConfigurationStore(options =>
    {
        options.ConfigureDbContext = builder =>
            builder.UseNpgsql(connectionString,
                sql => sql.MigrationsAssembly(migrationsAssembly));
    })
    .AddOperationalStore(options =>
    {
        options.ConfigureDbContext = builder =>
            builder.UseNpgsql(connectionString,
                sql => sql.MigrationsAssembly(migrationsAssembly));
        options.EnableTokenCleanup = true;
        options.TokenCleanupInterval = 30;
    });

services.AddAuthentication(IdentityServerConstants.DefaultCookieAuthenticationScheme)
    .AddIdentityServerAuthentication(options =>
    {
        options.Authority = EnvironmentReader.AuthorityUrl;
        options.ApiName = "api1";
        options.RequireHttpsMetadata = false;
    });

最佳答案

这是 AddDeveloperSigningCredential 与 AddSigningCredential 的问题。每次重新启动 IdentityServer 时使用 AddDeveloperSigningCredential, key Material 都会更改,所有使用先前 key Material 签名的 token 将无法验证。 “临时”实际上仅适用于您没有其他可用关键 Material 的情况。

以下内容来自此处的文档页面 Documentation

AddDeveloperSigningCredential

Creates temporary key material at startup time. This is for dev only scenarios when you don’t have a certificate to use. The generated key will be persisted to the file system so it stays stable between server restarts (can be disabled by passing false). This addresses issues when the client/api metadata caches get out of sync during development.

VS

AddSigningCredential

Adds a signing key service that provides the specified key material to the various token creation/validation services. You can pass in either an X509Certificate2, a SigningCredential or a reference to a certificate from the certificate store.

我的代码:

来 self 的配置的行

 services.AddIdentityServer()
            .AddSigningCredential(LoadCertificate())

额外方法

private X509Certificate2 LoadCertificate()
    {
        return new X509Certificate2("../../certs/TestCertificate.pfx",
            "pass");
    }

关于docker - 在 Linux 和 Azure 存储库上运行的 Docker 镜像中的 IdentityServer4 和身份验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50543487/

相关文章:

c# - Azure KeyVault : how to create clientId and clientSecret?

jwt - 使用 AWS API Gateway 时是否可以从自定义 Lambda 授权方传回 JWT 负载?

local-storage - 使用 JSON Web token 进行 CSRF 保护

docker - 有没有可能在非发行版镜像中安装bash?

ubuntu - Nginx client_max_body_size 在 AWS Elastic Beanstalk 上的 Docker 容器中不起作用

docker - 如何在一个mesos从节点上运行多个docker容器?

java - JWT 自定义过滤器中的身份验证管理器

php - 重建后文件更改未反射(reflect)在Docker镜像中

c# - Visual Studio 2017 缺少 "Add RestAPI Client"

asp.net-core - OpenIddict 在 token 响应中获取用户 ID