Hello, I would like to know, how can force user to redirect from http(not secured) page to https(secured page) in asp.net core?
I am using asp.net core boilerplate template.
In asp.net core, you can set up a redirect from HTTP to HTTPS on the web-server or use middleware Rewrite in your startup.cs
var options = new RewriteOptions()
.AddRedirectToHttps(StatusCodes.Status301MovedPermanently, 44384);
app.UseRewriter(options);
Complete code would look like this
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env)
{
var options = new RewriteOptions()
.AddRedirectToHttps(StatusCodes.Status301MovedPermanently, 44384);
app.UseRewriter(options);
app.UseMvcWithDefaultRoute();
}
In startup.cs you can configure Https redirect by using code below
public void ConfigureServices(
IServiceCollection services)
{
//add the MVC filter for https
services.AddMvc(options =>
{
options.Filters.Add(new RequireHttpsAttribute());
});
}
For those you want's to migrate http to https in asp.net MVC ,please refer this link's answer
https://qawithexperts.com/questions/140/how-to-redirect-from-http-to-https-in-aspnet
It has both the way's explained using web.config and IIS for ASP.NET and MVC
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly