IdentityServer4 необходимо настроить ключевой материал, но сертификат присутствует - PullRequest
0 голосов
/ 08 ноября 2019

Когда я публикую свое решение IdentityServer4 на своем IIS-сервере, в журнале появляется сообщение «System.Exception: необходимо настроить материал ключа». Это странно, потому что на моей локальной машине все работает нормально.

Я попытался утешить журнал выдачи сертификата и на моей машине это работает. Сертификат создается с помощью «KeyStore Explorer».

В файле appsettings.json я указал путь к сертификату и пароль. Я обновил параметр пути после развертывания на сервере, чтобы он правильно указывал на него. .

Это мой startup.cs:

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();
        }
    }
}

1 Ответ

0 голосов
/ 08 ноября 2019

Проблема была в Среде, потому что в локальной среде я использовал Разработка и на сервере Производство

...