当前位置 : 主页 > 网络编程 > ASP >

asp.net-core – 使用https / ssl的.net核心.我必须重定向吗?

来源:互联网 收集:自由互联 发布时间:2021-06-24
我现在在dotnet核心创建了一个网站.该网站现场直播,并在azure中托管.我已经设置了ssl sertificate,并将其绑定到该站点. 在web.config或启动中我有什么办法让ssl工作吗? 我无法使用https看到该
我现在在dotnet核心创建了一个网站.该网站现场直播,并在azure中托管.我已经设置了ssl sertificate,并将其绑定到该站点.

在web.config或启动中我有什么办法让ssl工作吗?

我无法使用https看到该网站.我必须在启动时重定向吗?

这是我最终得到的:

在startup.cs中,configure()

app.Use(async (context, next) =>
            {
                if (context.Request.IsHttps)
                {
                    await next();
                }
                else
                {
                    var withHttps = "https://" + context.Request.Host + context.Request.Path;
                    context.Response.Redirect(withHttps);
                }
            });
在启动时,您可以将整个站点配置为要求https,如下所示:

编辑:显示如何在生产中仅需要https但请注意,您可以轻松地在开发中使用https

public Startup(IHostingEnvironment env)
{
    ...
    environment = env;

}


public IHostingEnvironment environment { get; set; }

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.Configure<MvcOptions>(options =>
    {

        if(environment.IsProduction())
        {
            options.Filters.Add(new RequireHttpsAttribute());
         }

    });
}
网友评论