如何以编程方式枚举 Azure 订阅和租户?
How to enumerate Azure subscriptions and tenants programmatically?
如何以编程方式枚举 Azure 订阅和租户?这与我之前的问题有关 .
基本上我尝试在桌面或控制台应用程序中复制 Login-AzureRmAccount
和 Get-AzureRmSubscription
的行为。到目前为止,我发现 MSAL 似乎总是需要客户端 ID 和租户 ID,因此需要一些其他库来获取它们。在此之后,我想使用最新的库以编程方式创建服务主体,但我想这是一个需要进一步调查的主题(如果需要,还有问题)。
实际上,Login-AzureRmAccount
和Get-AzureRmSubscription
使用Microsoft Azure PowerShell应用程序通过Resource Manager REST APIs操作Azure资源。
要使用 REST 模拟与 PowersShell 命令相同的操作,我们也可以使用此应用程序。但是,由于此应用已在 Azure 门户(而非 v2.0 应用)上注册,因此我们无法通过 MSAL 使用此应用获取令牌。我们需要使用 Adal 而不是 MSAL。
这是一个代码示例,用于通过 Microsoft.WindowsAzure.Management 使用管理员帐户列出订阅,供您参考:
public static void ListSubscriptions()
{
string authority = "https://login.microsoftonline.com/common";
string resource = "https://management.core.windows.net/";
string clientId = "1950a258-227b-4e31-a9cf-717495945fc2";
Uri redirectUri = new Uri("urn:ietf:wg:oauth:2.0:oob");
AuthenticationContext authContext = new AuthenticationContext(authority);
var access_token = authContext.AcquireTokenAsync(resource, clientId, redirectUri, new PlatformParameters (PromptBehavior.Auto)).Result.AccessToken;
var tokenCred = new Microsoft.Azure.TokenCloudCredentials(access_token);
var subscriptionClient = new SubscriptionClient(tokenCred);
foreach (var subscription in subscriptionClient.Subscriptions.List())
{
Console.WriteLine(subscription.SubscriptionName);
}
}
更新:
string resource = "https://management.core.windows.net/";
string clientId = "1950a258-227b-4e31-a9cf-717495945fc2";
string userName = "";
string password = "";
HttpClient client = new HttpClient();
string tokenEndpoint = "https://login.microsoftonline.com/common/oauth2/token";
var body = $"resource={resource}&client_id={clientId}&grant_type=password&username={userName}&password={password}";
var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");
var result = client.PostAsync(tokenEndpoint, stringContent).ContinueWith<string>((response) =>
{
return response.Result.Content.ReadAsStringAsync().Result;
}).Result;
JObject jobject = JObject.Parse(result);
var token = jobject["access_token"].Value<string>();
client.DefaultRequestHeaders.Add("Authorization", $"bearer {token}");
var subcriptions = client.GetStringAsync("https://management.azure.com/subscriptions?api-version=2014-04-01-preview").Result;
Console.WriteLine(subcriptions);
如何以编程方式枚举 Azure 订阅和租户?这与我之前的问题有关
基本上我尝试在桌面或控制台应用程序中复制 Login-AzureRmAccount
和 Get-AzureRmSubscription
的行为。到目前为止,我发现 MSAL 似乎总是需要客户端 ID 和租户 ID,因此需要一些其他库来获取它们。在此之后,我想使用最新的库以编程方式创建服务主体,但我想这是一个需要进一步调查的主题(如果需要,还有问题)。
实际上,Login-AzureRmAccount
和Get-AzureRmSubscription
使用Microsoft Azure PowerShell应用程序通过Resource Manager REST APIs操作Azure资源。
要使用 REST 模拟与 PowersShell 命令相同的操作,我们也可以使用此应用程序。但是,由于此应用已在 Azure 门户(而非 v2.0 应用)上注册,因此我们无法通过 MSAL 使用此应用获取令牌。我们需要使用 Adal 而不是 MSAL。
这是一个代码示例,用于通过 Microsoft.WindowsAzure.Management 使用管理员帐户列出订阅,供您参考:
public static void ListSubscriptions()
{
string authority = "https://login.microsoftonline.com/common";
string resource = "https://management.core.windows.net/";
string clientId = "1950a258-227b-4e31-a9cf-717495945fc2";
Uri redirectUri = new Uri("urn:ietf:wg:oauth:2.0:oob");
AuthenticationContext authContext = new AuthenticationContext(authority);
var access_token = authContext.AcquireTokenAsync(resource, clientId, redirectUri, new PlatformParameters (PromptBehavior.Auto)).Result.AccessToken;
var tokenCred = new Microsoft.Azure.TokenCloudCredentials(access_token);
var subscriptionClient = new SubscriptionClient(tokenCred);
foreach (var subscription in subscriptionClient.Subscriptions.List())
{
Console.WriteLine(subscription.SubscriptionName);
}
}
更新:
string resource = "https://management.core.windows.net/";
string clientId = "1950a258-227b-4e31-a9cf-717495945fc2";
string userName = "";
string password = "";
HttpClient client = new HttpClient();
string tokenEndpoint = "https://login.microsoftonline.com/common/oauth2/token";
var body = $"resource={resource}&client_id={clientId}&grant_type=password&username={userName}&password={password}";
var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");
var result = client.PostAsync(tokenEndpoint, stringContent).ContinueWith<string>((response) =>
{
return response.Result.Content.ReadAsStringAsync().Result;
}).Result;
JObject jobject = JObject.Parse(result);
var token = jobject["access_token"].Value<string>();
client.DefaultRequestHeaders.Add("Authorization", $"bearer {token}");
var subcriptions = client.GetStringAsync("https://management.azure.com/subscriptions?api-version=2014-04-01-preview").Result;
Console.WriteLine(subcriptions);