Return to Snippet

Revision: 36197
at November 19, 2010 06:36 by dotNetkow


Initial Code
private static SforceService Login()
{
   // this is the web reference object generated by Visual Studio
   SforceService service = new SforceService();

   // Need to append password and security token together since we're logging in through the 
   // Salesforce API
   string passwordPlusSecurityToken = string.Format("{0}{1}","password","token");

   LoginResult loginResult;
   try
   {
      loginResult = service.login("salesforceUsername", passwordPlusSecurityToken);
   }
   catch (SoapException ex)
   {
      // Log error
      return null;
   }

   /** 
     * Once the client application has logged in successfully, it will use
     * the results of the login call to reset the endpoint of the service
     * to the virtual server instance that is servicing your organization
   */
   service.Url = loginResult.serverUrl;

   /** 
   * The client application now has an instance of the SforceService
   * that is pointing to the correct endpoint. Next, the sample client
   * application sets a persistent SOAP header (to be included on all
   * subsequent calls that are made with SforceService) that contains the
   * valid sessionId for our login credentials. To do this, the 
   * client application creates a new SessionHeader object and persists it to
   * the SforceService. Add the session ID returned from the login to the
   * session header.
   */
   service.SessionHeaderValue = new SessionHeader();
   service.SessionHeaderValue.sessionId = loginResult.sessionId;

   return service;
}

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

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

Initial Title
"Login" to Salesforce API using C#

Initial Tags
api, c#

Initial Language
C#