Salesforce Authentication with .NET (OAuth)

Salesforce gained a lot of attention as a CRM System and Platform over the last couple of years. As a .NET developer, you may have the challenge to connect to Salesforce with your application, even if it’s just transferring data via API.

While searching the web, you will find many tutorials on how to connect your .NET Application via the Salesforce API. However, some of the articles are outdated or a bit chatty.

tl;dr You find a full working copy of the code here https://github.com/abierhaus/salesforce-login-oauth-api-call

This tutorial gives you a quick start on connecting to Salesforce and making your first API call.

Create your Connected App in Salesforce

  1. Inside the Salesforce Setup, navigate to Apps — App Manager — New Connected App.
  1. In Name, type “Medium Connected Name” and supply a contact email.
  2. Check the “Enable OAuth Settings” checkbox.
  3. Set the callback URL to a dummy value, like http://localhost
  4. In Selected OAuth scopes, Add “Access and manage your data (API)”
  5. Leave other settings blank/ as they are and then create a connected app.

6. Click Continue. On the next page, write down the Consumer Key and the Consumer Secret. You will need it later for your app settings.

Create your Application in Visual Studio

  1. Create a new ASP.NET Core Application
  2. Create a default HomeController and register it in your Startup. cs

Configure appsettings

Create the following Configuration section within your appsettings.json (or better your User Secrets)

  1. Username and Password are your usernames or the username of your API User (recommended)
  2. You can reset a token on your “My personal Information Page” under “Reset My Security Token.”

3. The ClientId and the ClientSecret are the values from the managed app you created before. If you did not write it down, you could find it in your managed app here, where the Consumer Key is the Client Id, and the Consumer Secret is the Client Secret

4. You can leave the LoginEndpoint and ApiEndpoint as above. However, you should always use the latest ApiEndpoint version for your application. The API Version is increased for every Salesforce release.

Create a service class

This example encapsulates all Salesforce calls in a separate service class, which you can easily copy for your project. To provide maintainability, I make use of the current framework and naming standard here. Besides, we use as little as possible boilerplate code here.

You find the code here.

The underlying flow works like this:

a) Login to Salesforce with the credentials from the configuration/ appsettings and returning an object that contains the bearer token and the instance URL

b) Create HttpClient with a Get request and query the salesforce API endpoint that will return all available versions

c) Return result as a simple string

Don’t forget to register the newly generated service class in your StartUp.cs

Create HomeController

In the last step, create a simple HomeController and copy the code from here; you might need to register the route in your Startup.cs.

Now you can run the example and receive the Salesforce Versions back.

Entrepreneur — technologist — passionate leader