.net core3.1下接口发布成https协议操作步骤

1、在Startup.cs页面下,ConfigureServices方法里增加如下代码:

public void ConfigureServices(IServiceCollection services)
        {

.......

services.AddHttpsRedirection(InitHttpsRedirectionOptions);

.......

        }


        ///


        /// 初始化重定向参数
        ///

        ///
        public static void InitHttpsRedirectionOptions(HttpsRedirectionOptions httpsRedirectionOptions)
        {
            httpsRedirectionOptions.RedirectStatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status307TemporaryRedirect;
            httpsRedirectionOptions.HttpsPort = 5001;
        }

    ///


        /// 配置
        ///

        ///
        ///
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

          .......

         }

2、在linux上发布时,进行如下配置

.net core3.1下接口发布成https协议操作步骤_第1张图片

3、在启动服务时,会报如下的错误:

info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
crit: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to start Kestrel.
System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.

.net core3.1下接口发布成https协议操作步骤_第2张图片

4、执行语名:dotnet dev-certs https,如下图所示:

.net core3.1下接口发布成https协议操作步骤_第3张图片

 5、在浏览器里输入相应的地址https://xx.xx.xx.xx:8889/swagger,即可访问发布后的https接口页面。

 

 

你可能感兴趣的:(C#,c#)