Scott on Writing

Musings on technical writing...

Requiring HTTPS for Certain ASP.NET Pages

I'm currently working on a project that has SSL capabilities. Users reach the site through a partner site, with the user's session using SSL only if the partner site also used secure communications. There's also an administrative section that, regardless of whether or not the user entered using SSL, must be accessed via SSL. How, then, do you ensure that when a user visits a particular page or subset of pages that they do so via a secure channel?

There are a couple of techniques that I'm aware of. Probably the most sound way is to make the setting through IIS. If you go to the IIS metabase you can right-click on the folder or website you want to require be accessed only through SSL and go to Properties. Next, tab over to the Directory or File Security tab (depending on if you're configuring a directory or file) and, in the Secure Communications section, click the Edit button. This will bring up the Secure Communications dialog box; from there you can click the "Require secure channel" checkbox and, voila, the directory or file can now only be visited through HTTPS.

Once you have made this setting if a client attempts to visit such a configured resource through HTTP (rather than HTTPS) an HTTP Error 403.4 - Forbidden: SSL is required to view this resource.

You can also require SSL programmatically through your ASP.NET pages' code-behind classes, which can be useful if you don't have direct access to the web server's metabase to make the settings as described above). (Realize that setting the "Require secure channel" option through IIS has the advantage that it requires a secure channel for all types of resources - ASP.NET pages, HTML pages, images, and so on. By making this setting through ASP.NET - either in a code-behind class or in an HTTP Module - will only require SSL in resources requests that IIS hands off the the ASP.NET worker process.)

With the ASP.NET code-behind technique you basically add a bit of code to your code-behind class (or, better yet, a base class or HTTP Module) that checks to see if the request is through a secure channel; if not, it redirects the user to the same URL but through HTTPS. An example can be seen in this blog entry: 443 <--> 80 - Seamlessly moving requests in and out of SSL.

In my project I ended up using both techniques, actually. For the ASP.NET pages in the administrative interface I used a technique very similar to the one described in the blog entry. My main difference, though, was that I added a check to see if the incoming request was coming through localhost. If it was, then I didn't sweat the HTTP --> HTTPS translation. I just stayed with HTTP. (I did this because locally I do not have an SSL cert; sure, I could easily create and setup one, but why bother?) I went with the programmatic approach because the user might already 'legally' be on the site through HTTP and then click on a link to go to the admin page. I guess I could have went back and ensured that all admin links were fully qualified with URLs starting with https://, but instead I opted to let the person hit the admin page through HTTP only to be auto-redirected to the same page, but through HTTPS. I used the IIS approach in a couple of places where I had .htm files that needed to be protected. Also, I am using ELMAH on the site and wanted to ensure that its error log viewing page (elmah/default.aspx) could only be viewed through SSL, so potentially sensitive information couldn't 'accidentally' be sent over an insecure channel.

In closing, here are some additional resources I found on this topic that are worth reading:

posted on Wednesday, August 17, 2005 12:38 AM

Feedback

# re: Requiring HTTPS for Certain ASP.NET Pages 8/17/2005 1:48 PM Tod Birdsall

I have posted code that I use in my Global.asax to detect if the user is accessing the site using SSL and redirects them appropriately. You can see it here:
http://tod1d.blogspot.com/2005/08/redirect-browser-to-https.html

# re: Requiring HTTPS for Certain ASP.NET Pages 11/17/2005 8:56 AM Scott Mitchell

Scott Cate also has a free, source-code-included HTTPS-switching HTTP Module available:
http://scottcate.mykb.com/article_d2244.aspx

# re: Requiring HTTPS for Certain ASP.NET Pages 1/12/2006 12:38 AM Geert Roete

Thank you Scott! Just the information I was looking for. I need to switch easily between HTTP and HTTPS.

# re: Requiring HTTPS for Certain ASP.NET Pages 9/29/2006 4:32 PM Kevin

How are you maintaining your user credentials say from the ssl'd login page to a non-ssl'd page?
Everything I have tried looses the credentials. (Mind you I am trying to do this via windows/basic auth on the IIS side). I imagine even in forms auth. the credentials would be lost (at least I think) since the auth cookie was originally created in the ssl space. Now leaving the SSL space good-bye cookie thus goodbye credentials (AKA - principal object tied to page)....?

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<January 2009>
SMTWTFS
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1996.9%
Monday 41014.2%
Tuesday 48716.8%
Wednesday 53618.5%
Thursday 56119.4%
Friday 52118.0%
Saturday 1806.2%
Total 2894100.0%

Hour1Total% of Total
12:00 AM 712.5%
1:00 AM 782.7%
2:00 AM 652.2%
3:00 AM 782.7%
4:00 AM 622.1%
5:00 AM 1164.0%
6:00 AM 1123.9%
7:00 AM 1685.8%
8:00 AM 1826.3%
9:00 AM 1545.3%
10:00 AM 1816.3%
11:00 AM 1866.4%
12:00 PM 1966.8%
1:00 PM 1816.3%
2:00 PM 1655.7%
3:00 PM 1344.6%
4:00 PM 1133.9%
5:00 PM 1023.5%
6:00 PM 983.4%
7:00 PM 1043.6%
8:00 PM 893.1%
9:00 PM 832.9%
10:00 PM 852.9%
11:00 PM 913.1%
Total 2894100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.18145
Monday 5.01371
Tuesday 4.25463
Wednesday 7.54656
Thursday 6.71658
Friday 5.32431
Saturday 5.00170
Total 5.662894

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 6.3338
8:00 AM 4.52122
9:00 AM 6.04284
10:00 AM 6.14264
11:00 AM 4.30189
12:00 PM 6.75344
1:00 PM 3.12128
2:00 PM 5.23225
3:00 PM 8.36301
4:00 PM 3.7694
5:00 PM 5.89165
6:00 PM 4.56114
7:00 PM 9.15183
8:00 PM 8.53162
9:00 PM 5.00115
10:00 PM 6.3395
11:00 PM 4.5732
Total 5.662894

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles