Skip to content

Instantly share code, notes, and snippets.

@markcowl
Last active May 10, 2018 19:58
Show Gist options
  • Save markcowl/4d907da7ce40f2e424e8d0625887b82e to your computer and use it in GitHub Desktop.
Save markcowl/4d907da7ce40f2e424e8d0625887b82e to your computer and use it in GitHub Desktop.

Converting ServiceClientCredentials to SubscriptionCloudCredentials for Azure Management Clients

When using Azure SDK clients that depend on different runtimes (Microsoft.Azure.Common, Microsoft.Rest.ClientRuntime) together, authentication can be a problem, because the authentication libraries for both runtimes are highly encapsulated, and only one authentication should be required for all the Azure management clients used.

To resolve this, you can use an adapter like the following that converts ServiceClientCredentials (required for the Microsoft.Rest.ClientRuntime clients) to SubscriptionCloudCredentials (required by Microsoft.Azure.Common clients).

    public class SubscriptionCloudCredentialsAdapter : SubscriptionCloudCredentials
    {
        ServiceClientCredentials _wrappedCreds;
        string _subscriptionId;

        public SubscriptionCloudCredentialsAdapter(ServiceClientCredentials credentials, string subscriptionId)
        {
            _wrappedCreds = credentials;
            _subscriptionId = subscriptionId;
        }

        public override string SubscriptionId
        {
            get
            {
                return _subscriptionId;
            }
        }

        public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            return _wrappedCreds.ProcessHttpRequestAsync(request, cancellationToken );
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment