Created
December 17, 2018 07:25
-
-
Save AshV/45a8ed0c86a99d4e485f8d8bc7d843e1 to your computer and use it in GitHub Desktop.
Microsoft-Dynamics-365-Server-2-Server-Authentication-Access-Token.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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