"HTTP Error 401.1 – Unauthorized" when using Windows Authentication on IIS7.5

You may have run into this error while trying to develop a site that uses Integrated Windows Authentication on Windows Server 2008 R2 with IIS7.5.  I sure did, and I beat my head against the wall for a couple of hours trying to figure it out.

It turns out to be a security mechanism built into Windows, and the workaround is simple.  Just choose one of the two methods listed in this Microsoft KB article:

You can find a more detailed explanation here:

Hope this saves you some time.

(h/t to this ServerFault.com question/answer: IE 8 Authentication Denied on local SharePoint Site)

GraffitiCMS 1.2 export utility

Update 2010-09-29: This code has been moved to BitBucket: http://bitbucket.org/jonsagara/graffititoblogml

Disclaimer: This is a little one-off utility that I modified to export to mtimport, MovableType’s import format (the original version of the utility was written in VB.NET, and exported BlogML; this function still exists, though it is not called). It is not polished, it probably has a bug or two in it, and it likely does not do everything you need it to do. Unfortunately, I don’t have the time to make it do everything perfectly; I could only put in the time to make it good enough for me. That being said, the source code is available and is being released under the MIT License, so feel free to take it and modify it as you see fit.

Credit: This is a C# port and derivative of Curt C’s Graffiti To BlogML Exporter. He did the hard work of writing the original BlogML export routine. Thanks, Curt!

Dependencies: From your Graffiti CMS installation, you will need to copy DataBuddy.dll and Graffiti.Core.dll into the Solution Items folder of the attached Visual Studio 2008 solution. These are Telligent’s DLLs, and I’m pretty sure I don’t have rights to redistribute them, so you’ll need to get them yourself.  Also, you’ll obviously need Visual Studio 2008.

Usage:

  • Copy DataBudy.dll and Graffiti.core.dll from your Graffiti installation into the Solution Items folder
  • Open GraffitiToBlogML.sln in Visual Studio 2008
  • Edit the connection string in App.config to point to your Graffiti instance
  • Click the “…” button to select an output folder
  • Click the Run button
After execution finishes, you should have a viable mtimport.txt file that you can import into MovableType.  If you need a MovableType instance to test with, you can download one for free from JumpBox.
If WordPress is your final destination, then, after you have imported your data into MovableType, you can export it again (I recommend doing this extra step so that WordPress will be importing a file exported by MovableType itself, and not my little utility).  Once you have this new export file, WordPress can then import it.
If you’re an end user, I apologize for not providing a runnable executable, but time is money.  :)
If you have questions, please post them in the comments.  I will do my best to answer them, though I can’t guarantee any level of support.

Update 2009-09-22: The MovableType-to-WordPress importer built into WordPress does not appear to import tags. I found this tool that imports your tags after you have already done the main import from MovableType: http://www.simonecarletti.com/blog/2009/02/movable-type-to-wordpress-importer-utilities/

Another code snippet of the day

Today’s a good day. Check this out:

private static Dictionary<string, string> _dict = new Dictionary<string, string>();

static MyPage()
{
    //_dict.Add("item1", "val1");
    //_dict.Add("item2", "val2");
    //_dict.Add("item3", "val3");
    //_dict.Add("item4", "val4");
}

private void InitDictionary()
{
    try
    {
        _dict.Add("item1", "val1");
        _dict.Add("item2", "val2");
        _dict.Add("item3", "val3");
        _dict.Add("item4", "val4");
    }//END TRY
    catch
    {
        _dict.Clear();
        _dict.Add("item1", "val1");
        _dict.Add("item2", "val2");
        _dict.Add("item3", "val3");
        _dict.Add("item4", "val4");
    }//END CATCH
}

So what happened here? The guy decided that initializing the static dictionary variable in the static constructor wasn’t good enough, and that he needed to do it on every page request, so he created an instance method to do the initialization. For some reason, though, he kept getting an exception when initializing the dictionary after the first page view. Hmm… bettter just swallow the exception, clear the dictionary, and try again.

*slaps forehead*

Code snippet of the day

Just ran across this awesomeness today:

string campaignId = null;

/* ------------------------------------------------------------------------
 * see if the campaign id is in the request query params, so that a default
 * based on the url can be set.
*/
foreach (object key in Request.Params)
{
    string objCampaign = Convert.ToString(key);
    if (string.Compare(objCampaign, "campaignid", true) == 0)
    {
        campaignId = Request[Convert.ToString(objCampaign)];
        break;
    }//END IF
}//END FOREACH
/* ---------------------------------------------------------------------- */

Huh. Well, that’s one way to do a Request.QueryString["campaignid"] lookup.

Some more LINQ love

Here’s another example of how I feel LINQ improves the readability of my code. This is the before version, using standard looping to populate a list of objects:

private IEnumerable<Album> ReadAlbums(LLAlbum[] llAlbums)
{
    Album album;
    IList<Album> albums = new List<Album>(llAlbums.Length);

    foreach (LLAlbum llAlbum in llAlbums)
    {
        album = new Album()
        {
            AlbumId = llAlbum.Id
        };

        albums.Add(album);
    }

    return albums;
}

And here is the after version, using a LINQ query:

private IEnumerable<Album> ReadAlbums(LLAlbum[] llAlbums)
{
    return from llAlbum in llAlbums
           select new Album
           {
               AlbumId = llAlbum.Id
           };
}

Now, depending on your familiarity with LINQ, you may or may not agree with me, but I love it!

Project Euler: Problem 1

I just put my son down for a nap, so since I had a little spare time, I thought I’d try my hand at Project Euler.

Problem 1 was very simple. Right off the bat, I knew how I wanted to solve it:

int sum = 0;
for (int ix = 0; ix < 1000; ix++)
{
	if (ix % 3 == 0 || ix % 5 == 0)
	{
		sum += ix;
	}
}

However, I’ve been reading Jon Skeet’s excellent book, C# in Depth, so I wanted to see if I could solve it using LINQ:

int sum = Enumerable.Range(0, 1000)
	.Where(x => (x % 3 == 0 || x % 5 == 0))
	.Sum();

It still looks weird to me, but it works, so who am I to complain? :)