在.NET 6.0上使用Kestrel配置和自定义HTTPS

引用地址:在.NET 6.0上使用Kestrel配置和自定义HTTPS_kestrel 使用https_云策数据的博客-CSDN博客

在ASP.NET Core中,默认情况下HTTPS处于打开状态,这个不是问题,我们无需禁用它。因为如果你的服务是在防火墙后面,是属于后台服务,不对外网提供服务,启用HTTPS也是有意义的。

通常,在Windows上,启用HTTPS所需的证书是从Windows证书存储库加载的,在Linux或Mac上,则会从证书文件上加载证书,后者更加灵活,是我比较推荐的方式。

直接上代码

using System.Text.Encodings.Web;

using System.Text.Json;

using YC.Common.Core.Filter;

using YC.JsonSerialization;

using YC.UnifyResult;

using Newtonsoft.Json;

using Newtonsoft.Json.Serialization;

var builder = WebApplication.CreateBuilder(args).Inject();

builder.Host.UseSerilogDefault();

builder.Logging.AddJsonConsole(options =>

{

options.JsonWriterOptions = new JsonWriterOptions

{

Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping

};

});

builder.Services.AddControllersWithViews()

.AddMvcFilter<RequestActionFilter>()

.AddInjectWithUnifyResult<RESTfulResultProvider>()

.AddJsonOptions(options =>

{

options.JsonSerializerOptions.PropertyNamingPolicy = null;

})

.AddNewtonsoftJson(options =>

{

// 默认命名规则

options.SerializerSettings.ContractResolver = new DefaultContractResolver();

// 设置时区为 UTC

options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;

// 格式化json输出的日期格式

options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";

// 忽略空值

// options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;

// 忽略循环引用

options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

// 格式化json输出的日期格式为时间戳

options.SerializerSettings.Converters.Add(new NewtonsoftDateTimeJsonConverter());

});

var app = builder.Build();

app.Configuration.Get<WebHostBuilder>().ConfigureKestrel(options =>

{

// 长度最好不要设置 null

options.Limits.MaxRequestBodySize = 52428800;

});

app.Run();

AppSetting.json 设置

{

"AllowedHosts": "*",

// 配置扫描目录

"ConfigurationScanDirectories": [ "Configurations" ]

}

子目录设定

Kestrel.json 设定

{

"Kestrel": {

"Endpoints": {

"Http": {

"Url": "http://200.0.0.66:50001"

},

"HttpsInlineCertFile": {

"Url": "https://200.0.0.66:5000",

"Certificate": {

"Path": "I:\\source\\Net6\\yc-netcore-v3.4.1\\src\\Application\\YC.API.Entry\\all_wsb003_cn.pfx",

"Password": "Minke@2022"

}

}

}

}

}


版权声明:本文为CSDN博主「云策数据」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/bj_xuzhiqiang/article/details/126726816

相关推荐
追逐时光者17 小时前
一款基于 .NET 开源、功能强大的 Windows 搜索工具
.net
成富1 天前
MCP 传输方式,stdio、HTTP SSE 和 Streamable HTTP
网络·网络协议·http
Aczone281 天前
Linux 软件编程(九)网络编程:IP、端口与 UDP 套接字
linux·网络·网络协议·tcp/ip·http·c#
逃课的蟠桃1 天前
为什么访问HTTPS站点时,会发生SSL证书错误
网络协议·https
武文斌771 天前
计算机网络:网络基础、TCP编程
linux·网络·网络协议·tcp/ip·计算机网络
yuyuyui1 天前
Abp vNnext-事件总线使用实现及解析
.net·abp vnext
now_cn1 天前
HTTP 与 HTTPS:网络通信幽径上的安全秘钥
安全·http·https
mashanshui1 天前
Https之(二)TLS的DH密钥协商算法
算法·https·tls·dh·ecdhe
fs哆哆1 天前
在VB.net中一维数组,与VBA有什么区别
java·开发语言·数据结构·算法·.net
出海探索的Cindy1 天前
什么是IP隔离?一文讲清跨境电商/海外社媒的IP隔离逻辑
网络·网络协议·tcp/ip