Kerberos authentication can be really, really narky. What I found to solve the "run test as user x in .net" problem I needed to perform a two step process.
Step 1: Fire up the process I want to test as the alternate user.
Step 2: Use impersonation on the call to allow my test harness to access the process from the other user.
This was done for IE in WatiN, but I am guessing that the code will be similar, just calling your chosen test exe.
using System;
using System.Configuration;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Security;
using System.Net;
using System.Security.Principal;
using System.Reflection;
using System.IO;
public static void SetAlternateUser(string userName, string password, string domain)
{
// thread safe singleton code
lock (threadSafeLock)
{
if (_ie == null)
{
StartIE();
}
_ie.Close();
_ie = null;
// fill the NetworkCredeitials object that we use for impersonation
if (string.IsNullOrEmpty(domain))
{
alternateUserCredentials = new NetworkCredential(userName, password);
}
else
{
alternateUserCredentials = new NetworkCredential(userName, password, domain);
}
// Prepare to launch
ProcessStartInfo psi = new ProcessStartInfo();
psi.UserName = userName;
psi.Password = SecurePassword(password);
if (!string.IsNullOrEmpty(domain))
{
psi.Domain = domain;
}
psi.UseShellExecute = false;
psi.LoadUserProfile = true;
psi.FileName = "c:\\Program Files\\Internet Explorer\\iexplore.exe";
psi.Arguments = "about:blank";
// launch
Process proc = new Process();
proc.StartInfo = psi;
proc.Start();
// Time to become an imposter
hToken = IntPtr.Zero;
hTokenDuplicate = IntPtr.Zero;
if (Win32.LogonUser(alternateUserCredentials.UserName, alternateUserCredentials.Domain, alternateUserCredentials.Password, 2 /*LOGON32_LOGON_INTERACTIVE*/, 0 /*LOGON32_PROVIDER_DEFAULT*/, out hToken))
{
if (Win32.DuplicateToken(hToken, 2, out hTokenDuplicate))
{
windowsIdentity = new WindowsIdentity(hTokenDuplicate);
impersonationContext = windowsIdentity.Impersonate();
System.Threading.Thread.Sleep(1000);
_ie = IE.AttachToIE(Find.ByUrl("about:blank"));
}
}
}
}