1、在Startup.cs页面下,ConfigureServices方法里增加如下代码:
public void ConfigureServices(IServiceCollection services)
{
.......
services.AddHttpsRedirection(InitHttpsRedirectionOptions);
.......
}
/// <summary>
/// 初始化重定向参数
/// </summary>
/// <param name="httpsRedirectionOptions"></param>
public static void InitHttpsRedirectionOptions(HttpsRedirectionOptions httpsRedirectionOptions)
{
httpsRedirectionOptions.RedirectStatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status307TemporaryRedirect;
httpsRedirectionOptions.HttpsPort = 5001;
}
/// <summary>
/// 配置
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
.......
}
2、在linux上发布时,进行如下配置
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.
4、执行语名:dotnet dev-certs https,如下图所示:
5、在浏览器里输入相应的地址https://xx.xx.xx.xx:8889/swagger,即可访问发布后的https接口页面。