November 3, 2020

How to use SSO login while automating your application using selenium C#

  November 3, 2020

We may come across several situations in which we need to use our SSO access to login to an application.

But while launching a new browser using we will be always getting a fresh profile which does to consider our SSO.

So we need to create a browser profile which uses our existing user data and also the extensions which are needed to login with SSO such as Windows 10 Chrome extension

The following error will be shown on such issues

On Chromium/Edge:

You can't get there from here

This application contains sensitive information and can only be accessed from:

Devices or client applications that meet <Org Name> management compliance policy.

You need to be signed in to Microsoft Edge with the work or school account shown above. To sign in, click on your account image. Learn More

Sign out and sign in with a different account

More details


On Chrome:

You can't get there from here

This application contains sensitive information and can only be accessed from:

Devices or client applications that meet <Org Name> management compliance policy.

Since you're using Chrome, you need to install this extension. You must be on Windows 10 version 1703 and above. Alternatively, you can use Microsoft Edge or Internet Explorer to access this application.

Sign out and sign in with a different account

More details


In the case of chrome, we need to install the windows 10 extension also at first.

For this extension to work we may have to allow extensions also while creating a browser profile.


Now let's see how this works when we are using selenium C# to automate this process:

This can be controlled through the browser options we use while creating the driver

  • At first, we need to get the logged-in User Name, We can get it using the code below in C#
string currentUser = System.Environment.UserName;

  • Next, we need to use this in the driver options as an argument
ChromeOptions chromeOptions = new ChromeOptions();

chromeOptions .AddArguments(@"user-data-dir=C:\Users\" + currentUser + @"\AppData\Local\Google\Chrome\User Data");

This will create the browser profile with the current user data.

If we launch the browser with the above argument we can use the SSO which is part of our user data to login to the application.

We may need to provide access to Extensions also in the browser options as needed.

Incase we need to use the default profile(Containing bookmarks,saved password etc), Use the following line of code as user-data-dir 
chroptions.AddArguments(@"user-data-dir=C:\Users\" + loggedInUser + @"\AppData\Local\Google\Chrome\UserDataProfile\Default");

Complete code for a chrome browser launch is given below

1
2
3
4
5
6
7
8
string currentUser = System.Environment.UserName; //read the Logged in user name
ChromeOptions chroptions = new ChromeOptions();
chroptions.AddArguments("--noerrdialogs");
chroptions.AddArguments(@"user-data-dir=C:\Users\" + currentUser + @"\AppData\Local\Google\Chrome\User Data");

chroptions.AddAdditionalCapability("useAutomationExtension", false);
chroptions.AddArgument("no-sandbox");
driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), chroptions, TimeSpan.FromMinutes(3));

The same technique can be used in any other browser.

In some cases, we may need to clear the cache after each time we launch the browser. We can programmatically do that also along with browser launch if needed.
logoblog

Thanks for reading How to use SSO login while automating your application using selenium C#

Previous
« Prev Post

No comments:

Post a Comment

Fixing javascript error: Cannot read properties of null (reading 'querySelector') issue when using CSS identifier in selenium

 JavaScript is another way to interact with web elements when normal selenium methods fail to act. But one issue with javascript is, it does...