Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AshV/45a8ed0c86a99d4e485f8d8bc7d843e1 to your computer and use it in GitHub Desktop.
Save AshV/45a8ed0c86a99d4e485f8d8bc7d843e1 to your computer and use it in GitHub Desktop.
Microsoft-Dynamics-365-Server-2-Server-Authentication-Access-Token.cs
/// Required-Namespaces
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Threading.Tasks;
/// Method-to-generate-Access-Token
public static async Task<string> AccessTokenGenerator()
{
string clientId = "Azure AD App Id";
string clientSecret = "Client Secret Generated for App";
string authority = "https://login.microsoftonline.com/< your app tenant guid >";
string resourceUrl = "https://< your D365 org>.< crm instance location e.g crm, crm8 >.dynamics.com"; // Org URL
ClientCredential credentials = new ClientCredential(clientId, clientSecret);
var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority);
var result = await authContext.AcquireTokenAsync(resourceUrl, credentials);
return result.AccessToken;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment