Search code examples
c#asp.nethttpssecurityhttpcontext

Simplest way to Force HTTPS during Page_Init


On very few pages of my site i want to make sure they can only be accessed though HTTPS connections. What is the best way of accomplishing that. All the examples i see seem like they are trying to do it for the whole site.

I was hoping of finding a way to accomplish that on Page_Init or Page_Preload.

And redirect to Https version


Solution

  • You could access the IsSecureConnection property of the request and redirect:

    if (!Request.IsSecureConnection) {
        Response.Redirect(...);
    }
    

    You may have references to pages you want to redirect to, or you can construct an https Uri from the Request.Url parts, and so on, but there above you have the check.