Tag Archives: VB.NET

Forcing Logout in Forms Authentication

Here’s a simple fix to a common ASP.NET development problem. The only code you really need to put on a log out page is “FormsAuthentication.SignOut()”. However, any controls or code that change based on authentication (such as the LoginView control or the LoginStatus control) will not reflect that the user is logged out until the following web page. This is bad for usability because you may be presenting links that are technically un-clickable. I’ve seen forum posts that advocate redirecting to another page, but there’s a simpler fix:

Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
	FormsAuthentication.SignOut()
	Context.User = Nothing
End Sub

Setting the Context.User to nothing does the trick.

The hash race

On my earlier post this week about coding for speed, I said that reinventing the framework is bad. That is to say, it’s unlikely that some home-grown bit of code will outperform similar code that’s already been written by finer minds. There are always exceptions. I present to you a case in point: some experimental code on hashing binary data.

Hashing is useful when you need to check to see if any data has changed (a checksum), or if you need to generate a unique number from some arbitrary content. The standard solution to this problem is to generate an MD5 hash. Another more secure option is to generate a SHA hash. These are built into the .NET framework, so you don’t have to know anything other than you can feed a byte array in one end and get a small byte array out the other end. You can then take the byte array and turn it into a number or string or whatever. Another option is to do an XOR hash over the whole thing. A 32-bit example XOR hash function in VB.NET will follow this post.

I ran a short test to compare how long it would take to hash 1 million random bytes 1,000 times using MD5, SHA, and three versions of XOR: 64-bit, 32-bit, or 16-bit.

Hash race chart

In this case, 64-bit XOR hashing was half as slow (or twice as fast?) as MD5, just as MD5 was half the speed of SHA. The lesson is: be familiar with the framework to know your options, but code algorithms yourself when there are obvious speed advantages.
Read more »

Reinventing the framework is bad

There’s a bit of code on this site that regularly gets a few hits every day via search engines, namely my IP to country lookup code. BTW, WordPress is not so good at sharing code. There’s a function in that code that converts an IP string into a 64-bit integer (Int64). It does this by splitting an IP address into an array of four strings, then getting the hexadecimal value of those string, then joining the string back together and determining the numeric value. Sub-optimal, but it works.

It turns out that built into the .NET framework Net.IPAddress object, there are several methods that do this same thing but better. They are more robust, handle both IPv4 and IPv6 addresses, and the functions that are rewritten to use them run in about 1/3 the time (will post after the jump). My point is that before writing code, one should be familiar enough with the underlying framework to be sure that the same thing isn’t already written. One can get better code in less time.

Read more »

Testing with a local SSL certificate for free

It used to be that creating a self-signed certificate for local IIS debugging was a pain. Without a certificate, anything starting with “https://localhost” was bound to cause an error. There is a simple, two-minute fix that will work for most cases, however.

  1. Download and install the IIS Diagnostics Toolkit from Microsoft.
  2. Run the newly installed SSL Diagnostics program
  3. Right click on your local website and choose “Create new cert”. It will install a two-week locally signed certificate on your machine that is not technically valid, but will at least allow you to test SSL activity.

Read more »

Array.BinarySearch is good

I have a nice way to describe the binary search algorithm that is older-relative tested. How do you go about finding a particular card in a deck of 52 cards? Of course you flip through them one by one, and darn it if the card you’re looking for isn’t second to the last. If you have to find a single card often, you will quickly get tired of this. A better method would be to sort the cards in some predictable, understandable order. Then, when you need to find a certain card you divide the deck in half and see which half is closer to your card, then divide that pile in half and so on and so forth until you find it. In the worst case, you’ll only have to look at 7 cards. The beautiful thing is that if you doubled the number of cards, you’d only have to check your sorted deck one additional time. In a sorted deck of 8,000 unique cards, you’d only have to check 14 cards to find the one you’re looking for.

I mention this because I just posted a VB.NET class to look up IP addresses which would have been impractical without Array.BinarySearch. Enjoy.