How can I set up my page load event in my code behind to redirect to an url that has an https prefix? I would need it to work with urls that have query strings attached too.
It’s one thing to construct a link that goes straight to the https page, but I don’t want the user to be able to manually change it to an http page.
Also I don’t want to do it with javascript because it might be turned off.
I’m guessing a regular expression?
We mark our SSL Required pages with a special attribute
ForceSslAttribute. Then we have aHttpModulethat pulls down the current page’s class and inspect it’s attributes.If the attribute is present on the page, it takes the exact url that was passed and changes the protocol from
httptohttpsthen calls a redirect.There’s probably a bit simpler way of doing it, but that’s how it’s done for us.
Attribute:
Page Example (CodeBehind):
You can figure out the type of the page like this:
All
Page‘s implementIHttpHandlerand when you’re visiting a page, it’ll work.The cool part about this method is you can mark anything that’s an IHttpHandler and it’ll force the redirect too 🙂