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 );
}
}