3

I have been using some Java code to run API calls to do integrated testing on webservices in our SFDC org. So far, I used to log in through the browser and get my admin sessionId, then copy / paste it into the Java program, then log in as a non-admin user and get their sessionId and copy / paste that as well.

I have been trying to use the SOAP API to get both these sessionIds, and can easily get my admin sessionId, but I haven't been able to find a way to replicate the "Login as another user" functionality on the SFDC UI which lets you log in as another user without knowing their password.

Is there any way to do this? I generally use my admin creds to create a new user using an API and want to log in as that user immediately to get their sessionId.

Any pointers will be greatly appreciated...

  • So far I am thinking maybe I can create the new user, query for it, then use the setPassword() method on that user, then login as that user using the newly set password...? – jimpudar Jun 11 '16 at 21:49
  • This hasn't worked for me since I can't think of a way to get the user's security token :) – jimpudar Jun 11 '16 at 21:57
  • This is the exact question I have, but I guess this feature is not available. – abhi Jun 13 '16 at 15:01
3

As you know, there is a "login access" feature that can be used by user administrators, but this is only accessible through the user interface. Calling setPassword would allow you to log in as a specific user. You can avoid the Security Token requirement by adding/setting your current public IP address to the user's profile's IP range login restrictions (warning: do NOT do this to your administrator profile, lest you risk losing all administrator access to your org). There is a special "get a session token for any user" feature, but that feature is only available to salesforce.com employees, and has strict policies as to who can use it and under what circumstances, upon pain of termination.

| improve this answer | |
  • Is there a way to set the IP range login restrictions from the SOAP API? I am trying to avoid having to use the SFDC UI for this entirely. If there is a way to do this then this is a sufficient answer to the original question. – jimpudar Jun 12 '16 at 15:57
  • @jimpudar Not the SOAP API, but the Metadata API can do the trick. And, you can use the same Session ID for both APIs, so it's really quite trivial; just grab the existing profile, add some loginIpRange elements, and re-deploy (note: production deployments will cause all tests to run, so you might be waiting a bit every time you need to do this). – sfdcfox Jun 12 '16 at 16:13
  • Will generally only be using this on sandboxes so this is definitely an acceptable solution. Seems like using Selenium to log in using the UI might be a little faster though... – jimpudar Jun 12 '16 at 18:04
  • For anyone else trying to do this with a NEW user account - this is probably not going to be the correct route. Logging in as the user through the UI with Selenium will give you far better results. See this post for some more information... – jimpudar Jun 13 '16 at 11:31
2

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();
| improve this answer | |
-1

You should use the MetaData SOAP API. Here is how my C# code looks

var binding1 = new SoapClient();
binding1.Endpoint.Address = new EndpointAddress("https://test.salesforce.com/services/Soap/c/37.0/");
LoginScopeHeader soapHeader = new LoginScopeHeader { organizationId = "00D63000XXXX" };
var loginResults = binding1.login(new loginRequest(soapHeader, "abc@ab.com", "yourpassword"));

The only difference between regular user login and community user login is that you need to pass the LoginSoapHeader.

| improve this answer | |
  • 1
    It seems clear the OP does not know the user's password. – Adrian Larson Oct 13 '16 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.