Another solution would be to use impersonation. First, you need to unimpersonate (you cannot impersonate when you're already impersonating), by using the RevertToSelf() method. Then you can assume the 'new' identity by using the LogonUser API and impersonate with that user in domain.
Example:
[DllImport("advapi32.dll", SetLastError=true)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
/// <summary>
/// This method switches between users...
/// </summary>
/// <param name="username">The windows username</param>
/// <param name="domain">The domain this user belongs to</param>
/// <param name="password">The users password.</param>
private void SwitchUser( string username, string domain, string password)
{
IntPtr tokenHandle = new IntPtr(0);
// This is for the .NET 1.0 framework...
// IntPtr duplicateHandle = new IntPtr(0);
tokenHandle = IntPtr.Zero;
bool retVal = LogonUser( username,
domain,
password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
ref tokenHandle );
if( !retVal )
{
throw new Win32Exception();
}
// If you use <identity impersonate="true" /> in your web.config,
// you first need to unimpersonate (you can't impersonate when
// you're already impersonating...
System.Security.Principal.WindowsIdentity.Impersonate( IntPtr.Zero );
// For .NET 1.0 you need to use the DuplicateToken method to
// create a duplicate handle. In .NET 1.1 you don't need this
WindowsIdentity newID = new WindowsIdentity(tokenHandle);
WindowsImpersonationContext newUser = newID.Impersonate();
try {
// now, do the stuff you need to do with the new user account
TextBox1.Text = WindowsIdentity.GetCurrent().Name;
} finally {
// Go back to the original user.
newUser.Undo();
CloseHandle(tokenHandle);
}
}