Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 285797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:34:44+00:00 2026-05-12T05:34:44+00:00

I am trying to write Watin tests for an intranet application that uses Integrated

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:34:44+00:00Added an answer on May 12, 2026 at 5:34 am

    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

        [TestMethod]
        public void TestMethod()
        {
            SecureString password = new SecureString();
            password.AppendChar('p');
            password.AppendChar('a');
            password.AppendChar('s');
            password.AppendChar('s');
            password.AppendChar('w');
            password.AppendChar('o');
            password.AppendChar('r');
            password.AppendChar('d');
    
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.UserName = "localtest";
            psi.Password = password;
            psi.UseShellExecute = false;
            psi.LoadUserProfile = true;
            psi.FileName = "c:\\Program Files\\Internet Explorer\\iexplore.exe";
            psi.Arguments = "about:blank";
    
            Process proc = new Process();
            proc.StartInfo = psi;
            proc.Start();
    
            t.Join();
    
            proc.Kill(); 
        }
    
        private static void DoWorkAs(object o)
        {
            User u = o as User;
    
    
            IntPtr hToken = IntPtr.Zero;
            IntPtr hTokenDuplicate = IntPtr.Zero;
    
            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();
    
                    // domain\username
                    Console.WriteLine(" Thread 2 : " + WindowsIdentity.GetCurrent().Name);
    
                    IE ie = IE.AttachToIE(Find.ByUrl("about:blank"));
    
                    ie.GoTo(@"http://www.google.com/");
                    ie.TextField(Find.ByName("q")).TypeText("WatiN");
                    ie.Button(Find.ByName("btnG")).Click();
    
                    Assert.IsTrue(ie.ContainsText("WatiN"));
                    ie.GoTo("about:blank");
    
                    //revert
                    impersonationContext.Undo();
                    Console.WriteLine(WindowsIdentity.GetCurrent().Name);
                }
            }
            if (hToken != IntPtr.Zero) Win32.CloseHandle(hToken);
            if (hTokenDuplicate != IntPtr.Zero) Win32.CloseHandle(hTokenDuplicate);
        }
    
        public class User
        {
            public User(string u, string d, string p)
            {
                Domain = d;
                UserName = u;
                Password = p;
            }
            public string UserName;
            public string Domain;
            public string Password;
        }
        public class Win32
        {
            // P/Invoke snask
            [DllImport("advapi32.dll", SetLastError = true)]
            public static extern bool LogonUser(
                string lpszUsername,
                string lpszDomain,
                string lpszPassword,
                int dwLogonType,
                int dwLogonProvider,
                out IntPtr phToken);
    
            [DllImport("advapi32.dll", SetLastError = true)]
            public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int
               SECURITY_IMPERSONATION_LEVEL, out IntPtr DuplicateTokenHandle);
    
            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern bool CloseHandle(IntPtr hHandle);
    
        }
    

    This code needs a refactor, and won’work on Vista with IE7, because of an IE bug that was fixed in IE8.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a test that invokes a web service and tests it's
Trying to write a test for a helper method that uses acts_as_tree. Helper method
Trying to write a code at the moment that basically tests to see if
Trying to write a python application that downloads images from an RSS feed, and
I'm trying write a test for a method that returns a java.sql.Connection to connect
Im trying to write an exchange transport agent that tests to see if the
I'm trying write a Ruby script that checks if user credentials are valid using
I am trying write a function that generates simulated data but if the simulated
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
Trying to write a couple of functions that will encrypt or decrypt a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.