Showing posts tagged .NET

.NET, Basic Auth, 401 & … 301?

So that 30+ day thing got put on hold. Extended hold.

Anyway…

For a little while now I’ve been wanting to store some minor data files on my site behind a little “Basic Auth” wall, but I’ve been put-off because I couldn’t pull the files down with some simple .NET code. I would ALWAYS get a 401 response. Stupid Basic Auth, and stupid .NET and their weird authentication-mechanisms in HttpWebRequest objects, right?

I had previously tried passing along a NetworkCredentials object, or setting PreAuthenticate to true, or adding the Authorization header manually, or trying all three, or any combination thereof. Didn’t matter. Every. Damn. Time: (401) Unauthorized. So, that project got put on hold until today: I was determined to figure it out. I searched the web, I followed 12 or so links. Tried all of those things over again. All of it failed. Finally, I noticed someone was using WireShark to see what .NET was doing. Why didn’t I think of that? (don’t answer)

It turns out WireShark was the key to solving this! When I looked at the traffic, I noticed that before giving me a 401 back, my server was giving me a 301. Wait, 301? A permanent redirect? All .NET was showing me was the 401! Yup, it’ll do that.

The url I was using was…
http://soggybyte.com/adir/afile.txt

The 301 Location response pointed me to…
http://www.soggybyte.com/adir/afile.txt

SON OF A… all this time, my problem has been the damn dub-dub-dub.

So, let that be a lesson to all of you. I’m not entirely sure what the lesson is, perhaps it’s “WOOO WIRESHARK,” but there we go. I learned something today.