Ad-Hoc or Bulk Testing of Windows Login Credentials


I’m doing a SharePoint deployment right now, I’ve planned out the service accounts I’m going to use, handed off those requirements to the IT department, who responded back that, yep, they’d been done.

Call me paranoid but I’m thinking “did all those accounts get created with all the right passwords and user names”, I’m also thinking “I really don’t want to sit at my machine RDC’ing into the SharePoint box with that list of credentials”.

So I wrote a little app that lets you do ad-hoc user logins and which also lets you put a batch of logins into an XML file which it then uses as a credential testing source. For obvious reasons you’re not going to leave this type of file lying around anywhere, for very long 🙂

The source and solution can be downloaded here.

The app consists of 2 main parts;

The code to test a user login uses the impersonation code I wrote about here, which is shown here;

static bool TestUserLogin(string userAccount, string cleartextPassword)
{
	try
	{
		if (string.IsNullOrEmpty(userAccount) || string.IsNullOrEmpty(cleartextPassword))
			throw new ArgumentException("userAccount or Password is empty");

		var dua = userAccount.Split(new[] {'\\'});
		if (dua.Length != 2) throw new ArgumentException(@"userAccount must be in the form DOMAIN\UserAccount");

		var dom = dua[0];
		var uid = dua[1];
		using (var impersonator = new Impersonator(uid, dom, cleartextPassword, LogonType.LOGON32_LOGON_NETWORK, LogonProvider.LOGON32_PROVIDER_WINNT50))
		{
			// success
			Console.WriteLine(string.Format("  - {0}: Success", userAccount));
		}
		return true;
	}
	catch (Exception ex)
	{
		Console.WriteLine(string.Format("  - {0}: Error! \"{1}\"", userAccount, ex.Message));
	}
	return false;

I don’t need to return a bool from this code, but the reasons I’m doing so, leads us onto the next piece of rather amusing code which parses the XML file, for bulk testing of user logon credentials, as shown here;

static void TestCredentialFile(string credentialFile)
{
	try
	{
		if (string.IsNullOrEmpty(credentialFile))
			throw new ArgumentException("credentialFile is empty");

		XElement
			.Load(credentialFile)
				.Elements("user")
				.Select( un => TestUserLogin(un.Attributes("uid").First().Value,
														un.Attributes("pwd").First().Value) )
				.Count();
	}
	catch (Exception ex)
	{
		Console.WriteLine(string.Format("  - Error! \"{0}\"", ex.Message));
	}
}

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s