简体   繁体   中英

Problem with configuring ssl certificates in asp .net core API

I have the following certificates Certificates

I want to configure my asp .net core web API to use these certificates in order to use https redirection. I am using Kestrel for the app settings.

{
  "Kestrel": {
    "EndPoints": {
      "Https": {
        "Url": "https://jj.com:5002"
      }
    }
  }
}

I was trying to do it by myself while reading some online posts, but no result. Here is my Configure method in Startup.cs file.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerManager logger)
        {

            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "General API");
                c.SupportedSubmitMethods();
            });

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }


            app.UseHttpsRedirection();

            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action}");
            });
        }

Here is my Program.cs file content:

  public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .ConfigureServices((context, services) =>
            {
                services.Configure<KestrelServerOptions>(
                context.Configuration.GetSection("Kestrel"));


            }).ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                webBuilder.UseSetting("https_port", "443");

                webBuilder.ConfigureKestrel(o =>
                {
                    o.ConfigureHttpsDefaults(o =>
                o.ClientCertificateMode =
                    ClientCertificateMode.RequireCertificate);
                });

            }).UseWindowsService();

    }

Any help will be much appreciated !

If i understand you problem correctly, you just want to have https on kestrel. what you can do is to add following setting on appsetting.json

"HTTPS_PORT": "5151", "Kestrel": { "EndpointDefaults": { "Protocols": "Http1" }, "Certificates": { "Default": { "Path": "server_cert.pfx", "Password": "password" } } },

For this line o.ClientCertificateMode = ClientCertificateMode.RequireCertificate); Only use it if you want to use Client Certificate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM