How to redirect from http to https in ASP.NET Core?


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.


Asked by:- jaya
0
: 85 At:- 3/27/2018 12:48:17 PM
ASP.NET asp.net core






2 Answers
profileImage Answered by:- Sam

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());
            });
        }
2
At:- 3/28/2018 10:06:19 AM
thanks got it 0
By : jaya - at :- 4/3/2018 12:51:06 PM


profileImage Answered by:- vikas_jk

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

1
At:- 3/30/2018 11:27:03 AM Updated at:- 3/30/2018 11:27:41 AM
I wanted to migrate http to https in .NET Core, @Sam's answer worked for me, thanks 0
By : jaya - at :- 4/3/2018 12:51:44 PM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use