0
votes

I'm a little bit lost on how you can create credentials in the Azure .NET SDK without having to call the credentials from a local file.

In my case, I have a few subscriptions and I'm storing my data in a local database. I want to make multiple calls to Azure for my VMs using the credentials I store in the database.

They have numerous classes representing ways to authenticate in the SDK Documentation, but I can't see a clear way to create access tokens or use credentials (tenant id, subscription id, client id and secret) through the SDK.

For example, when calling one of the Client classes (ComputeManagementClient) you can call it with credentials to authenticate the request to Azure but they don't seem to provide a Class to generate the credentials beyond from a file.

Does anyone have an MSDN reference?

1

1 Answers

1
votes

Accoring to AzureCredentialsFactory class, we could know that we also could get the credentials FromServicePrincipal or FromUser. In your case I recomment that use the FromServicePrincipal and Microsoft.Azure.Management.Fluent to operate the Azure resource. I also do a demo for that.

Note: How to registry Azure AD Application and assign role please refer to this document.

var clientId = "clientId";
var secretKey = "secretKey";
var tenantId = "tenantId";
var subscriptionId = "subscriptionId";
var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, secretKey, tenantId,AzureEnvironment.AzureGlobalCloud);
ComputeManagementClient client = new ComputeManagementClient(credentials) {SubscriptionId = subscriptionId };
var result = client.VirtualMachines.ListAllAsync().Result;

enter image description here