在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"
        }
      }
    }
  }
}

Logo

更多推荐