function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RPanebiancoRPanebianco 

Login as User through API

Hi,

we are developing an app that use API to retrieve and update data from Salesforce.
Now we have a requirement related to Login As User. I want to understand if it is possible to use same functionality available on desktop to personify another user from app itself using API.

I'm trying to understand if an API or a flow that can be used from mobile app to simulate Login As User functionality. All references I found said that it is not supported. 

Do you have any suggestion or possible workaround to perform this case

Thanks
Raffaele 
Ajay K DubediAjay K Dubedi
Hi RPanebianco,
After much finagling I have determined the best way to do this is using Selenium to use the UI "Login as user" functionality.
First, I log in using SOAP API with my admin credentials from my Java code.
Then, using Selenium, I open a browser window as such:
WebDriver driver = new ChromeDriver();
driver.get(url + "/secur/frontdoor.jsp?sid=" + adminSessionId + "&retURL=" + url + "/" + contactId);

where url is the org's instance url and contactId is the contact I want to log in as. 
Using frontdoor.jsp will avoid the two factor authentication issues which you might have run into trying to use Selenium like this.

From here, we just need to click on the Manage External User dropdown and click the Login as User link:
driver.findElement(By.cssSelector("#workWithPortalButton")).click();
driver.findElement(By.name("networklogin")).click();

We have a visualforce page called SessionInfo (not in our prod org :D) which displays the current user's sessionId and username. 
So, now that the user is logged in, I simply navigate to that page and return the sessionId value.
driver.get(driver.getCurrentUrl() + "SessionInfo");
userSessionId = driver.findElement(By.id("j_id0:j_id1:sessionid")).getText().trim();

Now, I can close the browser window and the user is still logged in with the returned sessionId.
driver.quit();


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi