我正在尝试在本地C#.NET应用程序中实现功能。该应用程序将我的AAD凭据存储在变量(用户名和密码)中。
我似乎找不到任何方法来执行C#中PowerShell中Get-AzureRMResourceGroups的操作。我尝试使用Microsoft.Azure.Management.Fluent并没有成功。
我所能找到的就是如何访问Azure资源,传递数据等-我不感兴趣。我想列出资源组,资源及其权限。如果可能的话,我希望能够进行更改,但是现在我只想列出与订阅相关的信息。
谢谢,请不要犹豫编辑此问题以改进它。
答案 0 :(得分:2)
您将需要使用https://www.upscene.com/downloads/dbw,例如:
using Microsoft.Azure.Management.ResourceManager;
...
var tenantId = Environment.GetEnvironmentVariable("AZURE_TENANT_ID");
var clientId = Environment.GetEnvironmentVariable("AZURE_CLIENT_ID");
var secret = Environment.GetEnvironmentVariable("AZURE_SECRET");
var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
// Build the service credentials and Azure Resource Manager clients
var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, secret);
var resourceClient = new ResourceManagementClient(serviceCreds);
resourceClient.SubscriptionId = subscriptionId;
// Getting the resource groups
var groups=resourceClient.ResourceGroups.List().ToList();
答案 1 :(得分:2)
感谢铅@octavioccl
我用过:
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
clientId: clientId,
clientSecret: secretKey,
tenantId: tenantId,
environment: AzureEnvironment.AzureGlobalCloud
);
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithSubscription(subscriptionId: subscriptionId)
//.WithDefaultSubscription()
;
var lorg = azure.ResourceGroups.List();