加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 09:13:12 所属栏目:asp.Net 来源:网络整理
导读:我现在在dotnet核心创建了一个网站.该网站现场直播,并在azure中托管.我已经设置了ssl sertificate,并将其绑定到该站点. 在web.config或启动中我有什么办法让ssl工作吗? 我无法使用https看到该网站.我必须在启动时重定向吗? 这是我最终得到的: 在startup.c
我现在在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());
         }

    });
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读