locked
Run IE using different user credentials RRS feed

  • Question

  • Hi,

    I am using IE to run coded ui tests. To start the IE with default credentials I am using BrowserWindow.Launch(uri).

    Now I want to run same tests for multiple users on same machine so to run IE with different user credentials I tried using

    BrowserWindow browser = BrowserWindow.Launch(ProcessStartInfo) But this give invalid type cast error - 

    "Unable to cast object of type 'Microsoft.VisualStudio.TestTools.UITesting.ApplicationUnderTest' to type 'Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow'."

    So I tried ApplicationUnderTest browser = ApplicationUnderTest.Launch(ProcessStartInfo).

    This opens the IE but

    Your security setting level puts your computer at risk IE started with this message -
       
          We recommend that you do not browse the web with your current security settings.
           To browse the web securely, click Fix settings for me on the notification bar below.  
           To continue browsing in the current state, click the Home button (not recommended). 

    I am not sure what security settings are causing this and even when I change any IE setting, it gets reverted to default.
     
    Is there any way to run IE with different credentials to run coded ui test cases ?

     

    Thanks,

    Akshay

    Wednesday, January 4, 2012 1:14 PM

Answers

All replies

  • Hi Akshay,

    You can use this approach if it suits your requirement:

    try
    {
       path = pathToYourExecutable;//iexplorer.exe in your case
    
       ProcessStartInfo myProc = new ProcessStartInfo(path);
       myProc.Domain = domain;
       myProc.UserName = username;
       myProc.Password = password;
       myProc.UseShellExecute = false;
    
       Process.Start(myProc);
    }
    catch (Exception myException)
    {
       // error handling
    }
    

    You can get a handle to the launched window and use it for your subsequent steps.

    However, "Impersonation" would be a more appropriate answer to your query here. Go through some good articles on it and I am sure that will help resolving your issue.  Keep posting your queries :-)

    Regards,

    Pankaj

     

    • Edited by pankaj.nith Wednesday, January 4, 2012 1:55 PM
    Wednesday, January 4, 2012 1:53 PM
  • Hi Pankaj,

    Thanks for the help. I tried first approach provided by you, but I am getting same security error -

    Your security setting level puts your computer at risk IE started with this message -

    We recommend that you do not browse the web with your current security settings.
    To browse the web securely, click Fix settings for me on the notification bar below.
    To continue browsing in the current state, click the Home button (not recommended).

     

    About Impersonation, I implemented it through below code but now I when I try to start IE it gives Win32 Exception - Unknown Error (0xfffffffe)

     

     
    if (ImpersonateUser())
    {
     string path = @"C:\Program Files\Internet Explorer\iexplore.exe";
     ProcessStartInfo myProcess = new ProcessStartInfo(path);
     Process.Start(myProcess);  // Getting Error here.
    }

     

            public const int LOGON32_LOGON_INTERACTIVE = 2;
            public const int LOGON32_PROVIDER_DEFAULT = 0;

            [DllImport("advapi32.dll")]
            public static extern int LogonUserA(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);


            [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);

            [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool RevertToSelf();

            [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
            public static extern bool CloseHandle(IntPtr handle);

            #endregion


            public static bool ImpersonateUser()
            {
                WindowsIdentity tempIdentity;
               
                IntPtr token = IntPtr.Zero;
                IntPtr tokenDuplicate = IntPtr.Zero;

                string username = "Username";
                string password = "Password";
                string domain = "Domain";

                if (RevertToSelf())
                {
                   
                    if (LogonUserA(username, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                    {
                        if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                        {
                            tempIdentity = new WindowsIdentity(tokenDuplicate);
                            impersonationContext = tempIdentity.Impersonate();
                            if (impersonationContext != null)
                            {
                                CloseHandle(token);
                                CloseHandle(tokenDuplicate);
                                return true;
                            }
                        }
                    }
                }

                if (token != IntPtr.Zero)
                    CloseHandle(token);
                if (tokenDuplicate != IntPtr.Zero)
                    CloseHandle(tokenDuplicate);


                return false;
            }

     

     

    Thursday, January 5, 2012 6:54 AM
  • Hi,

    I want to start IE with different user credentials using impersonation but getting Win32 Exception - Unknown Error (0xfffffffe)

    Any help ?

    Impersonation Code

    if (ImpersonateUser())
    {
     string path = @"C:\Program Files\Internet Explorer\iexplore.exe";
     ProcessStartInfo myProcess = new ProcessStartInfo(path);
     Process.Start(myProcess); //This gives an expception
    }

     

            public const int LOGON32_LOGON_INTERACTIVE = 2;
            public const int LOGON32_PROVIDER_DEFAULT = 0;

            [DllImport("advapi32.dll")]
            public static extern int LogonUserA(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);


            [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);

            [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern bool RevertToSelf();

            [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
            public static extern bool CloseHandle(IntPtr handle);

            #endregion


            public static bool ImpersonateUser()
            {
                WindowsIdentity tempIdentity;
               
                IntPtr token = IntPtr.Zero;
                IntPtr tokenDuplicate = IntPtr.Zero;

                string username = "Username";
                string password = "Password";
                string domain = "Domain";

                if (RevertToSelf())
                {
                   
                    if (LogonUserA(username, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                    {
                        if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                        {
                            tempIdentity = new WindowsIdentity(tokenDuplicate);
                            impersonationContext = tempIdentity.Impersonate();
                            if (impersonationContext != null)
                            {
                                CloseHandle(token);
                                CloseHandle(tokenDuplicate);
                                return true;
                            }
                        }
                    }
                }

                if (token != IntPtr.Zero)
                    CloseHandle(token);
                if (tokenDuplicate != IntPtr.Zero)
                    CloseHandle(tokenDuplicate);


                return false;
            }

     


    Thursday, January 5, 2012 7:07 AM
  • Hi Akshay,

     

    I did some research. It seems that the Visual Studio and IE should be running under the same user credentials for the Coded UI Test to work properly. Like this blog http://blogs.msdn.com/b/siddharthapandey/archive/2009/12/13/testing-webpages-that-uses-windows-authentication-for-different-credentials.aspx We can force IE to prompt for username and password each time we navigate to a page that uses windows authentication. Hope it helps.

     

    Best Regards,

     


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 9, 2012 10:42 AM
    Moderator