How to Handle Basic Authentication Popup with Selenium C#

1,623 views
Skip to first unread message

tomdar...@gmail.com

unread,
Jul 6, 2022, 7:50:44 AM7/6/22
to Selenium Users
We have a Microsoft IIS (Windows Authentication) web site that pops up a Chrome/Edge browser window whenever we try and login.

We have implemented this C# Selenium code which works
driver.Navigate().GoToUrl("http://someUserName:Pass...@someurl.com/");

While the above code works, if some hacker sniffs port 80, they will be able to get the username and password to the site.

I found this Java article (https://medium.com/automationmaster/handling-basic-authentication-window-with-selenium-webdriver-and-devtools-api-ec716965fdb6) which I then converted to C#, but ti requires enabling Basic Authentication on the IIS Web site, which we don't want to do.

Question:  Does anyone out there know how to secure the UserName and Password?

Thanks 

Tom




tomdar...@gmail.com

unread,
Jul 6, 2022, 8:15:24 AM7/6/22
to Selenium Users
I tried this code and it errored out with error = "No such Alert"
            _chromeOptions = new ChromeOptions();
            _chromeOptions.AddArgument("--incognito");
            ChromeDriver driver = new ChromeDriver(_chromeOptions); //opens Chrome
            driver.Navigate().GoToUrl("http://myInternalWindowsAuthURL");
                driver.SwitchTo().Alert();  //code errors out here with error = "No Such Alert"
                driver.FindElement(By.Id("UserName")).SendKeys(username);
                driver.FindElement(By.Id("Password")).SendKeys(password);
                driver.SwitchTo().Alert().Accept();
                driver.SwitchTo().DefaultContent();

joseph...@gmail.com

unread,
Jul 6, 2022, 6:30:27 PM7/6/22
to Selenium Users
The fact that adding username:password@URL is working for you means that whatever is causing that login popup to appear is using Basic Authentication. If that's coming from your IIS Web site, it's already configured for Basic Authentication and is using it.

Jim Evans, the main Selenium developer for C# code, wrote a series of blog posts about how to handle many types of authentication (and why they work this way).  You can find the first post, with links to all of the other posts, here:

tomdar...@gmail.com

unread,
Jul 7, 2022, 12:07:57 PM7/7/22
to Selenium Users
I found a solution using AutoIt.
Here is the script for AutoIt,  Works like a champ!

WinWaitActive("[CLASS:Chrome_WidgetWin_1]")

Send("USERNAME{TAB}PASSWORDFORUSERNAME{TAB}")

Local $hWnd = WinWait("[CLASS:Intermediate D3D Window1]", "", 2)

ControlClick($hWnd, "", "Intermediate D3D Window1")

Send("{ENTER}")


Cheers



Reply all
Reply to author
Forward
0 new messages