sindrem
sindrem

Reputation: 1231

.net core using https/ssl. Do i have to redirect?

Ive created a website in dotnet core now. The site is live in, and hosted in azure. Ive set up the ssl sertificate, and binded it to the site.

Is there anything i have to do in web.config or startup to make ssl work?

I cant see the site using https. Will i have to redirect in startup?

Here is what i ended up with:

inside 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);
                }
            });

Upvotes: 3

Views: 1435

Answers (2)

S.A.
S.A.

Reputation: 495

With release Asp NET Core 1.0.0 update your Startup class:

public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddMvc();

    services.Configure<MvcOptions>(options =>
    {
        options.Filters.Add(new RequireHttpsAttribute());
    });

    // ...
}

Upvotes: 1

Joe Audette
Joe Audette

Reputation: 36736

in startup you can configure the entire site to require https like this:

EDITED: to show how to only require https in production but note that you can easily also use https in development

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());
         }

    });
}

Upvotes: 6

Related Questions