Skip to content

Managed Identity Support #234

@fowl2

Description

@fowl2

It's fairly trivial^ to hook up Azure.Identity in code, but it'd be nice if the connection string supported managed identity directly.

^ I'm not sure this handles token caching correctly

    var factory = new AzureIdentityOrgSvcFactory(new ("https://....dynamics.com"), new Azure.Identity.DefaultAzureCredential(), logger);

    class AzureIdentityOrgSvcFactory : IOrganizationServiceFactory
    {
        readonly Uri uri;
        readonly TokenCredential tokenCredential;
        readonly ILogger logger;

        public OrgSvcFactory(Uri uri, TokenCredential tokenCredential, ILogger logger)
        {
            this.logger = logger;
            this.uri = uri;
            this.tokenCredential = tokenCredential;
        }

        public IOrganizationService CreateOrganizationService(Guid? userId)
        {
            var c = new Microsoft.PowerPlatform.Dataverse.Client.ServiceClient(uri, GetToken, logger: logger);

            if (userId is Guid callerId)
                c.CallerId = callerId;

            return c;
        }

        async Task<string> GetToken(string instance)
        {
            var option = new TokenRequestContext(new[] { uri.AbsoluteUri + ".default" });
            var token = await tokenCredential.GetTokenAsync(option, default);
            return token.Token;
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions