我的解决方案由三个项目组成,它们是:
- 托管 IdentityServer 的 ASP.NET MVC Core 项目。
- 一个 protected 并管理 IdentityServer 的 ASP.NET Core API 项目。
- 另一个调用 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/