Integrate Salesforce with C# or ASP.NET MVC Application
Introduction
This tutorial explains the manual steps to authenticate Salesforce account using C# or ASP.NET MVC application. Even though the sample provided is C# Console application, we have implemented the same with ASP.NET MVC application for RainWorx's AuctionWorx software.
Pre-requisites
1. Salesforce Test Account
2. Visual Studio (C#)
Manual Steps for SalesForce Registration
1.Let's start by creating a new project with name - "SalesForceAuthSample". Open Visual Studio, New Project -> Visual C# -> Windows -> Console Application
Figure 1: Visual Studio > New Project
2. Create Salesforce test acccount.
3. Add references for "DeveloperForce.force" using NuGet Package Manager in your project.
4. For Salesforce Integration, we need following information such as Security Token, Consumer Key, Consumer Secret, Username, Password and IsSandBoxUser.
Security Token: We will get Security Token from the following location - Dropdown with Username located at top right corner > My Settings > Personal > Reset My Security Token. On clicking "Reset Security Token" button , the new Security Token will be sent on registered email address.
Figure 2: Salesforce > Security Token
Consumer Key and Consumer Secret: We will get Consumer Key and Consumer Secret from the following location - Set Up > Create > App > New. Enter all required data. Callback URL is very important for authentication purpose and then Save it.
Figure 3: Salesforce > Consumer Secret and Consumer Key
After saving the API, it will show page similar to screenshot given below.
Click on Connected App Name and you will get Consumer Secret and Consumer Key.
Figure 5: Salesforce > Connected App Details
Username and Password: The Username and Password will be your test Salesforce account login username and password.
IsSandBoxUser: In code given below, we will set this parameter to "true".
using Salesforce.Common; using Salesforce.Force; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SalesForceAuthSample { class Program { private static readonly string SecurityToken = System.Configuration.ConfigurationManager.AppSettings["SecurityToken"]; private static readonly string ConsumerKey = System.Configuration.ConfigurationManager.AppSettings["ConsumerKey"]; private static readonly string ConsumerSecret = System.Configuration.ConfigurationManager.AppSettings["ConsumerSecret"]; private static readonly string Username = System.Configuration.ConfigurationManager.AppSettings["Username"]; private static readonly string Password = System.Configuration.ConfigurationManager.AppSettings["Password"]; private static readonly string IsSandboxUser = System.Configuration.ConfigurationManager.AppSettings["IsSandboxUser"]; static void Main(string[] args) { try { var task = RunSample(); task.Wait(); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); var innerException = e.InnerException; while (innerException != null) { Console.WriteLine(innerException.Message); Console.WriteLine(innerException.StackTrace); innerException = innerException.InnerException; } } } private static async Task RunSample() { try { var auth = new AuthenticationClient(); // Authenticate with Salesforce Console.WriteLine("Authenticating with Salesforce"); var url = IsSandboxUser.Equals("true", StringComparison.CurrentCultureIgnoreCase) ? "https://login.salesforce.com/services/oauth2/token " : "https://test.salesforce.com/services/oauth2/token"; await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password, url); Console.WriteLine("Connected to Salesforce"); var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion); // retrieve all accounts Console.WriteLine("Get Accounts"); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } } } }
In next blog, I will explain about simple CRUD operations which we can perform on Salesforce.
Link - Salesforce CRUD operations using C# in AuctionWorx (ASP.NET MVC application)
Happy Coding :)