.net核心使用https/ssl.我必须重定向吗?

sin*_*rem 3 asp.net-core

我现在在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);
                }
            });
Run Code Online (Sandbox Code Playgroud)

Joe*_*tte 6

在启动时,您可以将整个站点配置为要求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());
         }

    });
}
Run Code Online (Sandbox Code Playgroud)