Last active
March 22, 2019 21:32
-
-
Save justlikemichel/5f605cfac460c4ce039811ce6d19bf6a to your computer and use it in GitHub Desktop.
This file contains 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
using Microsoft.IdentityModel.Clients.ActiveDirectory; | |
using System; | |
using System.Collections.Generic; | |
namespace MichelOverrideExampleApp | |
{ | |
public class MichelOverrideExampleHookImplementation : Microsoft.Xrm.Tooling.Connector.IOverrideAuthHookWrapper | |
{ | |
// In memory cache of access tokens | |
Dictionary<string, AuthenticationResult> accessTokens = new Dictionary<string, Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult>(); | |
public void AddAccessToken(Uri orgUri, Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult accessToken) | |
{ | |
// Access tokens can be matched on the hostname, | |
// different endpoints in the same organization can use the same access token | |
accessTokens[orgUri.Host] = accessToken; | |
} | |
public string GetAuthToken(Uri connectedUri) | |
{ | |
// Check if you have an access token for this host and it's not expired | |
if (accessTokens.ContainsKey(connectedUri.Host) && accessTokens[connectedUri.Host].ExpiresOn > DateTime.Now) | |
{ | |
return accessTokens[connectedUri.Host].AccessToken; | |
} | |
else | |
{ | |
// Try to get or refresh the token | |
accessTokens[connectedUri.Host] = GetAccessTokenFromAzureAD(connectedUri); | |
} | |
return null; | |
} | |
private AuthenticationResult GetAccessTokenFromAzureAD(Uri orgUrl) | |
{ | |
// TODO: Add your authentication implementation here | |
return null; | |
} | |
} | |
public class MichelOverrideExampleProgram | |
{ | |
static void Main(string[] args) | |
{ | |
// Define organization Url | |
Uri orgUrl = new Uri("https://organizationname.crm.dynamics.com"); | |
// Call your existing authentication implementation | |
Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult accessToken = GetAccessTokenFromAzureAD(orgUrl); | |
// Create instance of your hook | |
var hook = new MichelOverrideExampleHookImplementation(); | |
// Add token to your hook | |
hook.AddAccessToken(orgUrl, accessToken); | |
// Register the hook with the CrmServiceClient | |
Microsoft.Xrm.Tooling.Connector.CrmServiceClient.AuthOverrideHook = hook; | |
// Create a new instance of CrmServiceClient, pass your organization url and make sure useUniqueInstance = true! | |
var client = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(orgUrl, useUniqueInstance: true); | |
if (!client.IsReady) | |
{ | |
// Connection failed, report error | |
Console.Error.WriteLine(client.LastCrmException?.Message ?? client.LastCrmError); | |
} | |
else | |
{ | |
// Connection success | |
// TODO Add your code here | |
//client.Retrieve("account", Guid.Empty, new ColumnSet("accountid")); | |
} | |
} | |
private static AuthenticationResult GetAccessTokenFromAzureAD(Uri orgUrl) | |
{ | |
// TODO: Add your authentication implementation here | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment