I am trying to write Watin tests for an intranet application that uses Integrated Authentication. The web page that I am trying to test prints Page.User.Identity.Name.
Here is some of the code from my test:
if (Win32.LogonUser(u.UserName, u.Domain, u.Password, 2 /*LOGON32_LOGON_INTERACTIVE*/, 0 /*LOGON32_PROVIDER_DEFAULT*/, out hToken))
{
if (Win32.DuplicateToken(hToken, 2, out hTokenDuplicate))
{
WindowsIdentity windowsIdentity = new WindowsIdentity(hTokenDuplicate);
WindowsImpersonationContext impersonationContext = windowsIdentity.Impersonate();
Console.WriteLine(WindowsIdentity.GetCurrent().Name);
using (IE ie = new IE(url))
{
Console.WriteLine(ie.ContainsText(u.UserName));
ie.AutoClose = false;
}
impersonationContext.Undo();
}
}
When I run this, it prints the user name that I am trying to impersonate to the console, but the web page displays the user that I am currently logged in as, not the user that I should be impersonating.
Similar issue found at:
Automated testing of authorization scenarios implemented with AzMan
Impersonation is tricky and I have never been able to get IE to run as another user context with WatiN. In the past I have deployed another version of the site being tested with basic authentication enabled and then logged in via the dialog.
Have a look at the following blogs for more info and sample code:
http://blogs.msdn.com/jimmytr/archive/2007/04/14/writing-test-code-with-impersonation.aspx
http://blogs.msdn.com/shawnfa/archive/2005/03/21/400088.aspx
Edit: I got this working today. the trick is that you need to separate the launching of IE and the automation of IE as you can't just do them both in one hit.
First launch ie using System.Diagnostics.Process. Once you have launched IE you can then use the code from here to attach and talk to IE using impersionation
Here is the code
This code needs a refactor, and won'work on Vista with IE7, because of an IE bug that was fixed in IE8.