tags:

views:

515

answers:

2

I have a website (www.mydomain.com) that is secured with an SSL certificate. It is an ASP.NET website and I have forced certain pages via code to be required to use the https:// prefix. If they don't it will redirect them to the https:// equivalent. Is this a good practice? Is there an easier way to do this? Not every single page requires SSL.

Also, when the users use my URL in the form of mydomain.com instead of www.mydomain.com they get a certificate error because the certificate was registered for www.mydomain.com. Should I use the same approach as I am with the http:// and https:// issue I mentioned above? Or is there a better way of handling this?

+2  A: 

Your approach sounds fine. In my current project, I force HTTPS when a user goes to my login page, (Based on a config flag which lets me test locally without dealing with needing a cert). This allows me to access other pages unsecured which is handy.

I have a couple places where our server grabs the output of other pages (rendering to html to PDF and fetching dynamic images for example). Because of our environment, our server can't resolve it's public name, so if we were to force ssl at the site we'd have to add, our internal IP address (or fake the domain name).

As for your second question you have two options to handle the www.example.com vs example.com. You can buy a certificate that allows you to have multiple domain names. These are known as UCC certificates.

Your second option is to redirect example.com to www.example.com or the other way around. Redirecting is a great option if want your content to be indexed by google or other search engines. Since they will see www.example.com and example.com as two seperate sites. This means that links to your sites will be split reducing your overall page rank.

JoshBerke
Yes, I take the same approach. I have a EnableSsl key in my web.config file which I can turn off and on depending on what environment I am in. I also have a pipe-delimited SecurePages key that I can easily add/remove sites to.
Mike C.
Another way instead of Pipe-delimited (Assuming you dont need to change this after you deploy) would be to define a base page that does the check, then any page you want to guarentee secure will always inherit from that page. You could also use attributes.
JoshBerke
Yeah but I would rather keep one single base page, and I would like to ability to dynamically set the pages to ssl or not without recompiling.
Mike C.
+1  A: 

You can configure sites in IIS to require a Cert but that would A) generate an error if someone isn't visiting with https and B) require all pages to use https. So, that won't work. You could put a filter on IIS that checks all requests and redirects them as https calls if they are on your encryption list. The obvious drawback here is the need to update your list of pages every time a new page is added (e.g. from an XML file or database) and restart the filter.

I think that you are probably correct in building code into the pages that require https that redirects to an https version if they arrive via http. As far as your cert error goes, you could redirect with a full path (that includes the www) instead of a relative path to fix this problem. If you have any questions about how to detect whether the call uses https OR how to get the full path of the current request please let me know. Both are pretty straightforward but I've got sample code if you need it.

UPDATE - Josh, the certs that handle multiple subdomains are called wildcard certs. The problem is that they are quite a bit more expensive than standard certs.

UPDATE 2: One other thing to consider is to use a Master page or derived class for the pages that need SSL. That way, instead of duplicating the code in each page you can just declare it as type SSLPage (or use the corresponding Master page) and have the Master/Parent class handle the redirect. Again, you'll need to do some URL processing if you take this approach but it is pretty trivial.

Mark Brittingham
Thank you for your input. I am already detecting for http:// and https://, so that won't be a problem. I'll have to play around with checking for www. because the url won't have that on my development machine. Any advice?
Mike C.
Mark now they are called UCC certificates. Wildcards are different. A UCC from Godaddy that can support 5 domains is $89 / year.
JoshBerke
Mike: Add www.localhost entry to your hosts file. This will let you test your www. logic :-)
JoshBerke
Josh - are UCCs different from Wildcards? My support guy does this now but I thought he still called them Wildcards - and ours lets us have any number of subdomains. In any event, this is valuable stuff, thanks!
Mark Brittingham