Tuesday, September 20, 2011

Force SSL in VB.NET

Today I'm wrapping up a project to convert an existing web site to use SSL communication only. This required adding HTTPS detection in various places to prevent mixed-content errors. I'm not entirely happy with my solution for that yet, so I'm not going to go into that right now.

What I do want to talk about is actually forcing the user over to HTTPS once your site is SSL-ready. The code I had used for many years on classic ASP sites was not working reliably; it would redirect, but to the home page instead of the page you tried to access.

ASP.NET has beefed up the Request object with some additional information we didn't have back in the old days (Request.IsSecureConnection and Request.Url). We also have a new way of including our function libraries, the App_Code folder.

  1. NameSpace myApplication
  2.     Public Class myLibrary
  3.         Public Shared Sub ForceSSL()
  4.             If Not System.Web.HttpContext.Current.Request.IsSecureConnection Then
  5.                 System.Web.HttpContext.Current.Response.Redirect(System.Web.HttpContext.Current.Request.Url.AbsoluteUri.Replace("http://", "https://"))
  6.             End If
  7.         End Sub
  8.     End Class
  9. End NameSpace


Then from each page on the site I import my custom library and run the subroutine:

  1. Imports myApplication.myLibrary
  2. Sub Page_Load(sender as Object, e as EventArgs)
  3.     ForceSSL()
  4.     'Rest of code goes here...
  5. End Sub


Supposedly there's a way to do this without code by modifying some settings in IIS, but I didn't have any success with that. The particular situation I'm dealing with has some fairly severe cohesion/coupling issues with certain pages being accessed from different subdomains.

Download the VB.NET source code for ForceSSL from Snipplr.com

Tuesday, May 10, 2011

Temperature Conversion Revisited

I recently completed another ASP.NET project and learned about the app_code folder, which lets you share code between pages. It's slightly similar to the #include directive in Classic ASP, but a lot more structured. I couldn't find much good documentation on it, so most of what I've learned has been through experimentation.

Any files placed into the app_code folder are automatically available from any page in your web application, so you can write: myNamespace.myClass.myFunction(someVariable) and the magic will happen. Alternatively, you can add Imports myNamespace.myClass in the top of your page and call myFunction without all the prefixing.

Though I'm still not a big fan of ASP.NET, this app_code thing is pretty slick and it's inspired me to write more reusable code. I had previously written a collection of functions for temperature conversion, which I've since grown unhappy with after reading Code Complete 2.0 by Steve McConnell because I named my functions like this: CelsiusToFahrenheit, instead of like this: FahrenheitFromCelsius.

I had been planning to write more conversion functions for various units of measurement, but was never satisfied enough with how it was turning out to make it publically available. Besides learning to appreciate ASP.NET, I'm also learning to appreciate object-oriented programming. This whole namespace/class combination with app_code seems to be appropriately suited for doing unit of measurement conversion.

So here's my plan. I've defined a namespace called UOM, which is the manufacturing industry standard abbreviation for unit of measure. Each type of measurement will be a class: temperature, distance, volume, mass, etc. Each class will accept string input so that you can specify both the numeric quantity and the unit of measure simultaneously. The class will use the specified unit of measure to convert values to a common unit of measure, and be able to return values in any equivalent unit of measure.

VB.NET

  1. Imports UOM
  2. Dim outside as Temperature
  3. outside = New Temperature("10 C")
  4. Response.Write(outside.getFahrenheit) 'Displays "50"

And so I proudly announce the release of the first installment in the UOM namespace: UOM.Temperature, which handles Celsius, Delisle, Fahrenheit, Kelvin, Newton, Rankine, Réaumur, and Rømer temperature scales.

Download the VB.NET source code for UOM.Temperature from Snipplr.com