欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

IdentityServer4 Keyset is missing报错

程序员文章站 2022-07-06 19:55:51
...

IdentityServer4 Keyset is missing报错

一直很懒没有记录学习过程中发现的问题,希望以后自己能多记录这些过程。

由于自己英文很渣,一般都是边写边用翻译软件翻译,当看到AddDeveloperSigningCredential时就不淡定了,以为是开发时用的临时证书生成语句,结果就把它Delete了,哪成想一运行报错。
ERROR|Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware|An unhandled exception has occurred while executing the request. System.InvalidOperationException: Policy error while contacting the discovery endpoint http://localhost:5000/.well-known/openid-configuration: Keyset is missing
估计没几个人会像我这样写代码,一般都是照抄,我有洁癖喜欢把用不到的代码都清理掉,结果在中文网站上愣是没找到解决办法,最后终于在https://*.com/questions/47709265/identity-server-4-error-keyset-is-missing上找到这个问题。哎!!
下面是代码,记录一下。

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ArchivesContext>(options => {
                var connectionString = Configuration.GetConnectionString("Connection");
                options.UseMySQL(connectionString);
            });
            services.AddScoped<IArchivesRepository, ArchivesRepository>();
            services.AddIdentityServer()
                .AddDeveloperSigningCredential()
                .AddInMemoryIdentityResources(Resources.GetIdentityResourceResources())
                .AddInMemoryApiResources(Resources.GetApiResources())
                .AddInMemoryClients(Clients.GetClients())
                .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>();

            services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
                .AddIdentityServerAuthentication(options =>
                {
                    options.Authority = $"http://{Configuration["Identity:IP"]}:{Configuration["Identity:Port"]}";
                    options.RequireHttpsMetadata = false;
                    options.ApiName = "WebApi";
                    options.ApiSecret = "DAWebApi";
                });
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseIdentityServer();
            
            app.UseMvc();
        }
    }
相关标签: IdentityServer 4