C# IdentityServer 4需要配置关键材料,但证书存在
当我在IIS服务器上发布IdentityServer4解决方案时,日志会显示“System.Exception:需要配置密钥材料”。这很奇怪,因为在我本地的机器上它工作得很好 我试图通过控制台记录证书颁发者的日志,并在我的机器上运行。证书是使用“密钥库资源管理器”生成的 在appsettings.json中,我放置了证书的路径和密码。在服务器上部署之后,我更新了path参数,因此它应该正确指向它 这是我的startup.cs:C# IdentityServer 4需要配置关键材料,但证书存在,c#,.net,iis,ssl-certificate,identityserver4,C#,.net,Iis,Ssl Certificate,Identityserver4,当我在IIS服务器上发布IdentityServer4解决方案时,日志会显示“System.Exception:需要配置密钥材料”。这很奇怪,因为在我本地的机器上它工作得很好 我试图通过控制台记录证书颁发者的日志,并在我的机器上运行。证书是使用“密钥库资源管理器”生成的 在appsettings.json中,我放置了证书的路径和密码。在服务器上部署之后,我更新了path参数,因此它应该正确指向它 这是我的startup.cs: namespace IdentityServer { pub
namespace IdentityServer
{
public class Startup
{
public IHostingEnvironment Environment { get; }
public static IConfiguration StaticConfig { get; private set; }
public Startup(IHostingEnvironment environment, IConfiguration configuration)
{
Environment = environment;
StaticConfig = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
// uncomment, if you want to add an MVC-based UI
//services.AddControllersWithViews();
//core 2.2
services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_2);
var fileName = Path.Combine(Environment.ContentRootPath, StaticConfig.GetValue<string>("CertFilePath"));
if (!File.Exists(fileName))
{
throw new FileNotFoundException("Signing Certificate is missing!");
}
//This is where I import the cert
var cert = new X509Certificate2(fileName, StaticConfig.GetValue<string>("CertPassword"));
//This is where I log the issuer
Console.WriteLine("Issuer: " + cert.IssuerName);
var builder = services.AddIdentityServer()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApis())
.AddInMemoryClients(Config.GetClients())
.AddSigningCredential(cert); //This is where I add the cert
if (Environment.IsDevelopment())
{
builder.AddDeveloperSigningCredential();
}
else
{
//Here the Exception my server throw
throw new Exception("need to configure key material");
}
}
public void Configure(IApplicationBuilder app)
{
if (Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// uncomment if you want to support static files
//app.UseStaticFiles();
app.UseIdentityServer();
// uncomment, if you want to add an MVC-based UI
//app.UseEndpoint(endpoints =>
//{
// endpoints.MapDefaultControllerRoute();
//});
//core 2.2
app.UseMvcWithDefaultRoute();
}
}
}
名称空间标识服务器
{
公营创业
{
公共IHostingEnvironment环境{get;}
公共静态IConfiguration StaticConfig{get;private set;}
公共启动(IHostingEnvironment环境、IConfiguration配置)
{
环境=环境;
StaticConfig=配置;
}
public void配置服务(IServiceCollection服务)
{
//如果要添加基于MVC的UI,请取消注释
//services.AddControllersWithViews();
//核心2.2
services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_2);
var fileName=Path.Combine(Environment.ContentRootPath,StaticConfig.GetValue(“CertFilePath”);
如果(!File.Exists(fileName))
{
抛出新的FileNotFoundException(“缺少签名证书!”);
}
//这是我导入证书的地方
var cert=new X509Certificate2(文件名,StaticConfig.GetValue(“CertPassword”);
//这是我记录发行人的地方
Console.WriteLine(“发卡机构:+cert.IssuerName”);
var builder=services.AddIdentityServer()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.getAPI())
.AddInMemoryClients(Config.GetClients())
.AddSigningCredential(cert);//这是我添加证书的地方
if(Environment.IsDevelopment())
{
AddDeveloperSigningCredential();
}
其他的
{
//这里是我的服务器抛出的异常
抛出新异常(“需要配置关键材料”);
}
}
公共void配置(IApplicationBuilder应用程序)
{
if(Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//如果要支持静态文件,请取消注释
//app.UseStaticFiles();
app.UseIdentityServer();
//如果要添加基于MVC的UI,请取消注释
//app.UseEndpoint(端点=>
//{
//endpoints.MapDefaultControllerOute();
//});
//核心2.2
app.UseMvcWithDefaultRoute();
}
}
}
问题出在环境上,因为在本地,我使用的是开发和服务器生产