簡體   English   中英

我可以從本地 C# 應用程序列出 Azure 資源組嗎?

[英]Can I list Azure resource groups from a local C# application?

我正在嘗試在本地 C#.NET 應用程序中實現一個功能。 該應用程序將我的 AAD 憑據存儲在變量(用戶名和密碼)中。

我似乎找不到任何方法來做 Get-AzureRMResourceGroups 在 PowerShell 中所做的事情,在 C# 中。 我曾嘗試使用 Microsoft.Azure.Management.Fluent 沒有任何成功。

我能找到的只是如何訪問 Azure 資源、傳遞數據等等——我不感興趣。我想列出資源組、資源及其權限。 如果可能,我希望能夠更改它們,但現在我只想列出訂閱相關信息。

謝謝,請隨時編輯此問題以改進它。

您將需要使用ResourceManagementClient ,例如:

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

感謝領導@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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM