Posted By

dotNetkow on 11/19/10


Tagged

api salesforce c#


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Priestd09


"Login" to Salesforce API using C#


 / Published in: C#
 

URL: http://wiki.developerforce.com/index.php/Integrating_Force.com_with_Microsoft_.NET

"Log in" to Salesforce via this code. Borrowed heavily from URL reference.

  1. private static SforceService Login()
  2. {
  3. // this is the web reference object generated by Visual Studio
  4. SforceService service = new SforceService();
  5.  
  6. // Need to append password and security token together since we're logging in through the
  7. // Salesforce API
  8. string passwordPlusSecurityToken = string.Format("{0}{1}","password","token");
  9.  
  10. LoginResult loginResult;
  11. try
  12. {
  13. loginResult = service.login("salesforceUsername", passwordPlusSecurityToken);
  14. }
  15. catch (SoapException ex)
  16. {
  17. // Log error
  18. return null;
  19. }
  20.  
  21. /**
  22.   * Once the client application has logged in successfully, it will use
  23.   * the results of the login call to reset the endpoint of the service
  24.   * to the virtual server instance that is servicing your organization
  25.   */
  26. service.Url = loginResult.serverUrl;
  27.  
  28. /**
  29.   * The client application now has an instance of the SforceService
  30.   * that is pointing to the correct endpoint. Next, the sample client
  31.   * application sets a persistent SOAP header (to be included on all
  32.   * subsequent calls that are made with SforceService) that contains the
  33.   * valid sessionId for our login credentials. To do this, the
  34.   * client application creates a new SessionHeader object and persists it to
  35.   * the SforceService. Add the session ID returned from the login to the
  36.   * session header.
  37.   */
  38. service.SessionHeaderValue = new SessionHeader();
  39. service.SessionHeaderValue.sessionId = loginResult.sessionId;
  40.  
  41. return service;
  42. }

Report this snippet  

You need to login to post a comment.