SalesforceClient
.HttpClient
class, which is the easiest way to make HTTP callouts. It offers great support for asynchronous calls, but for this post, we’ll be making the calls synchronous by calling the Result keyword.https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=YOURCONSUMERID&redirect_uri=https://www.yourappname.com/salesforce/callback
https://www.yourappname.com/salesforce/callback?code=aWekysIEeqM9PiThEfm0Cnr6MoLIfwWyRJcqOqHdF8f9INokharAS09ia7UNP6RiVScerfhc4w%3D%3D
callback()
method to authenticate onto our connected app. This method accepts the parameter code.Note: Salesforce requires us to use FormUrlEncodedContent
or it will complain of a bad request.
HttpClient client = new HttpClient();
var response = new HttpResponseMessage();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"grant_type", "authorization_code "},
{"client_id", YOUR_CONSUMER_Id}, //We will recevied from connected app
{"client_secret", YOUR_CONSUMER_SECRET}, //We will recevied from connected app
{ "redirect_uri", YOUR_WEBAPP_URL+"/SalesforceCallBack"}, //Callback URL
{"code",Code } //We will recevied while calling the Authorization
});
response = client.PostAsync(Url, content).Result;
string jsonResponse = response.Content.ReadAsStringAsync().Result;
var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonResponse);
AuthToken = values["access_token"];
InstanceUrl = values["instance_url"];
Let’s see an example request:
login.salesforce.com/services/oauth2/token?grant_type=authorization_code&redirect_uri=https://www.yourappname.com/salesforce/callback&client_id=YOUR_CONSUMER_ID&client_secret=YOUR_CONSUMER_SECRET&code=aWekysIEeqM9PiThEfm0Cnr6MoLIfwWyRJcqOqHdF8f9INokharAS09ia7UNP6RiVScerfhc4w%3D%3D
The response to our authentication request a JSON object containing the authentication token and the instance URL (where to direct all subsequent queries). We store these in the properties created above.
Response: {
"access_token" : "00D2v000001h1le!ARcAQDI9sqDLgzHr4zSZDzltr...",
"instance_url" : "https://varianceinfotech.salesforce.com",
"id" : "https://login.salesforce.com/id/00D2v000001h1le7EAE/005...",
"token_type" : "Bearer",
"issued_at" : "1569926610464",
"signature" : "TmoeKs2s70SgVNxQSay..."
}
Refersh token example call:
https://login.salesforce.com/services/oauth2/token?grant_type=refresh_token&client_id=YOUR_CONSUMER__ID&client_secret=YOUR_CONSUMER__SECRET&refresh_token=YOUR_REFRESH_TOKEN
That’s it :-) We have authenticate our Salesforce App with .Net web project.
Cheers!
We’ve seen that calling the REST API from .Net is quite easy, especially with the help of the HttpClient
class. The main challenges are around authenticating with Salesforce, the complexities of OAuth and the concept of the connected app.