【发布时间】:2018-02-17 20:56:50
【问题描述】:
注册 Azure Active Directory 应用程序后,我在 Azure 门户中生成了一个应用程序 ID 和一个密钥。
通过这对和我的 Powershell 脚本,我想生成一个访问令牌,我将在 HTTP 请求的脚本中使用它。
我可以使用 C# 获取访问令牌(参见下面的代码),但我不确定如何在 Powershell 中执行类似操作。 Powershell中是否已经内置了任何东西?如果没有,有人可以指出我的解决方案吗?我很确定我不是唯一一个有这个问题的人。
public static async Task<string> GetAccessTokenAsync(string clientId, string appKey, string resourceId)
{
string aadInstance = "https://login.microsoftonline.com/{0}";
string tenant = "<TENANT>.onmicrosoft.com";
string authority = string.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
AuthenticationContext authContext = new AuthenticationContext(authority);
ClientCredential clientCredential = new ClientCredential(clientId, appKey);
AuthenticationResult authenticationResult = null;
authenticationResult = await authContext.AcquireTokenAsync(resourceId, clientCredential);
return authenticationResult.AccessToken;
}
【问题讨论】:
标签: c# powershell azure azure-active-directory access-token