简体   繁体   中英

Managing Credentials in Azure .NET Core 2.0 SDK

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?

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;

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM