简体   繁体   中英

Selenium C# - exception when run selenium driver after impersonate

I am using windows functionality, impersonate, to change the active user before starting Chrome driver. Now, starting the driver without the impersonate code works fine. Also the impersonate code works fine; I see the user is changed. But when this change happens and after that I run IWebDriver driver=new ChromeDriver then the exception is triggered on that exact code and the test stops. Any ideas why this happens?

Here is main part of the code used (the code is just little modified code from another post here at stackoverflow)

namespace localSeleniumTest.Impersonation
{
    class Program
    {
        [DllImport("advapi32.dll", SetLastError = true)]
        public static extern bool LogonUser(stringpszUsername, string pszDomain, string pszPassword,
            int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

        // closes open handes returned by LogonUser
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public extern static bool CloseHandle(IntPtr handle);

        public void Impersonation()
        {
            WindowsImpersonationContext impersonationContext = null;
            IntPtr userHandle = IntPtr.Zero;
            const int LOGON32_PROVIDER_DEFAULT = 0;
            const int LOGON32_LOGON_INTERACTIVE = 2;
            string domain = Config.domain;
            string user = Config.username;
            string password = Config.password;

            try
            {
                String currentName = WindowsIdentity.GetCurrent().Name;

                // if domain name was blank, assume local machine
                if (domain == "")
                    domain = System.Environment.MachineName;

                // Call LogonUser to get a token for the user
                bool loggedOn = LogonUser(
                    user,
                    domain,
                    password,
                    LOGON32_LOGON_INTERACTIVE,
                    LOGON32_PROVIDER_DEFAULT,
                    ref userHandle
                    );

                if (!loggedOn)
                {
                    return;
                }

                // Begin impersonating the user
                impersonationContext = WindowsIdentity.Impersonate(userHandle);

                String afterImpersonationName = WindowsIdentity.GetCurrent().Name;


                /*this few lines below does not work after impersonation but 
work perfectly without the code above.*/
                    IWebDriver driver = new ChromeDriver();
                    driver.Navigate().GoToUrl("www.google.com");
                    System.Threading.Thread.Sleep(6000);
                    driver.Quit();

Found the issue. D user did not have permission to chrome driver i Bin folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM