我大部分基于我对this post的工作,奠定了一些基础的。
您需要先在Azure AD中创建本机应用程序并添加Windows Azure服务管理API权限。
的客户端ID 从该应用程序获得的。
这是我目前使用获得令牌可以与管理SDK中使用的代码:
string userName = "yourUserName";
string password = "yourPassword";
string directoryName = "yourDirectory.onmicrosoft.com";
string clientId = "{ClientId obtained by creating an App in the Active Directory}";
var credentials= new UserPasswordCredential(string.Format("{0}@{1}", userName, directoryName), password);
var authenticationContext = new AuthenticationContext("https://login.windows.net/" + directoryName);
var result = await authenticationContext.AcquireTokenAsync("https://management.core.windows.net/", clientID, credentials);
var jwtToken = result.AccessToken;
//Example accesing Azure Cdn Management API
string subscriptionId = "xxxx-xxxxxx-xxxx-xxxxxxx";
using (var cdn = new CdnManagementClient(new TokenCredentials(jwtToken)) { SubscriptionId = subscriptionId })
{
//do something...
}
你的目录名可以在Azure门户可以获得> Azure的AD部分,上域名。
我的应用程序是在蔚蓝的网站托管:https://AppHostedName.azurewebsites.net/ 我的例子目录名称将是“AppHopstedName”? –
您可以在Azure上看到目录名称,转到Azure AD服务并检查**域名**。我编辑了包含屏幕截图的答案。 –
谢谢Matias,你帮了我很多 –