如何创建可以访问WebSiteManagementClient的Azure凭据?

时间:2016-02-15 12:54:23

标签: azure azure-web-sites credentials azure-active-directory azure-sdk-.net

我想使用Azure SDK中的WebSiteManagementClient,但是我在第一个障碍中摔倒 - 创建客户端。我已经按照David Murray提供的说明创建了一个Active Directory应用程序,这使我可以创建一个TokenCredential,我成功地用它来创建SQLManagementClient的SQL数据库。

ClientCredential cc = new ClientCredential("{myApplicationId}", "{myAzurePassword}");
var context = new AuthenticationContext("https://login.windows.net/{myTennantId}");
var result = context.AcquireToken("https://management.azure.com/", cc);
if (result == null)
    throw new InvalidOperationException("Failed to obtain the JWT token");
TokenCloudCredentials CloudCred = new TokenCloudCredentials(subscriptionId, result.AccessToken);
var sqlClient = new SqlManagementClient(CloudCred);
...
// and this can be used to create databases in resource groups

WebSiteManagementClient似乎需要一组不同的参数。首先,它需要一个ServiceClientCredential,然后是一些DelegatingHandlers。我从哪里获得ServiceClientCredential?什么是委派处理程序。我找了一个博客,其中有一个例子,但没有成功。我非常感谢任何指针。 感谢

1 个答案:

答案 0 :(得分:1)

继续我的搜索后,我找到并回答了类似问题here,但这次访问了ComputeManagementClient。在测试了解决方案后,我意识到我非常接近。我只需要使用result.AccessToken创建凭证,而不是使用SubscriptionID创建凭证,如下所示

ClientCredential cc = new ClientCredential("{myApplicationId}", "{myAzurePassword}");
var context = new AuthenticationContext("https://login.windows.net/{myTennantId}");
var result = context.AcquireToken("https://management.azure.com/", cc);
if (result == null)
    throw new InvalidOperationException("Failed to obtain the JWT token");
TokenCredentials Cred = new TokenCredentials(result.AccessToken);
var sqlClient = new SqlManagementClient(CloudCred);
...
// and this can be used to create databases in resource groups

我要感谢Noah Stahl,他提供了答案