3

I have a web application build in asp.net which runs on both HTTP and HTTPS.

I want to make it run only on HTTPS.

Any body having idea what changes it will involve

I have no idea about this?

Microsoft DN
  • 9,706
  • 10
  • 51
  • 71

4 Answers4

1

In your web server, Just Redirect the HTTP urls to corresponding HTTPS urls. Please check http://www.jppinto.com/2010/03/automatically-redirect-http-requests-to-https-on-iis7-using-url-rewrite-2-0/

Thanga
  • 7,811
  • 3
  • 19
  • 38
  • I tried to implement this approach but after making all settings as mentioned in the link, web site still runs on http instead of redirecting to https. – Microsoft DN Dec 23 '15 at 14:09
1

You can add that code in your Global.asax.cs file

protected void Application_BeginRequest()
{
  if (FormsAuthentication.RequireSSL && !Request.IsSecureConnection)
  {
    Response.Redirect(Request.Url.AbsoluteUri.Replace("http://", "https://"));
  }
}
adam.bielasty
  • 671
  • 9
  • 20
  • I tried this solution and is working well. But if I add Application_BeginRequest(), my web page loads multiple times. Whats the reason behind that? – Microsoft DN Dec 23 '15 at 11:05
1

In the Global.asax.cs simply add this code. Consider Request.IsLocal for local development.

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext context = HttpContext.Current;
    if (!context.Request.IsSecureConnection && !context.Request.IsLocal)
    {
        Response.Redirect(context.Request.Url.ToString().Replace("http:", "https:"));
    }
}

Read more about www and not www redirects here and here:

Community
  • 1
  • 1
dimonser
  • 675
  • 6
  • 12
  • I tried this solution and is working well. But if I add Application_BeginRequest(), my web page loads multiple times. Whats the reason behind that? – Microsoft DN Dec 23 '15 at 11:05
  • I can't understand what `my web page loads multiple times` means. If you request **http** it will be redirected to a **https**, so 2 requests will be send to a server. If you request **https** then `Response.Redirect` doesn't occurs – dimonser Dec 23 '15 at 12:34
  • Yes I understand 2 requests will be send to server. But after these 2 requests one more time the page loads but without entering in `Application_BeginRequest`. I tried creating a sample web site and adding `Application_BeginRequest` it loads the page multiple times, dont understand why? – Microsoft DN Dec 23 '15 at 13:42
  • I have made a sample website and it works fine. Please, can you share your code with me? – dimonser Dec 23 '15 at 14:17
  • I have created a simple website through visual studio (File -> New Website) without any control on the page. Just added above code in global.asax. I am testing this on local so removed `!context.Request.IsLocal ` condition for the above code. When I run this, control enters in `Application_BeginRequest` only once, but the website loads multiple times. It first loads when website runs for very first time. The loading symbol then stops and again started loading without entering in `Application_BeginRequest`. This happens multiple time. – Microsoft DN Dec 24 '15 at 08:36
0

The easiest way to do this is in IIS.

Under the website select 'SSL Settings' and tick the 'Require SSL' checkbox and click apply.

The benefits of this approach are it's safer (no chance of a bug in your code allowing non SSL requests) and it's easier and faster to implement. The drawbacks are that requests to HTTP URLs will not be automatically redirected, but you didn't state this is a requirement.

If it is a requirement, it's also possible to implement this using the IIS URL Rewrite module.

Ian Newson
  • 7,679
  • 2
  • 47
  • 80
  • I tried implementing URL Rewrite module but after making all settings as mentioned in the link, web site still runs on http instead of redirecting to https – Microsoft DN Dec 23 '15 at 14:09
  • @MicrosoftDN In the link? I didn't post a link. Did you comment (and down vote) on the wrong post? – Ian Newson Dec 23 '15 at 15:32