从 Powershell 获取 Azure Active Directory 访问令牌
Get Azure Active Directory Access Token from Powershell
我在注册 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;
}
我就此写了一篇我认为很棒的教程:
Azure AD Authentication with PowerShell and ADAL
With these scripts, you can get authentication and REST API calls done
with as little as 13 lines of PowerShell. Running the code is instant,
and modifying the REST calls or even the authentication parameters
takes seconds rather than minutes.
你可以在 GitHub here.
上找到我所有的脚本
诀窍是您实际上可以使用 PowerShell 的 Add-Type
函数加载 ADAL 的 .NET 程序集:
# Load ADAL
Add-Type -Path ".\ADAL\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"\
从那里开始,调用所有 ADAL 函数就像在标准 C# 应用程序中一样简单。
话虽这么说,您可能想要了解的另一件事是官方 AAD PowerShell 模块:
Azure Active Directory PowerShell Version 2
You can use the Azure Active Directory PowerShell Module Version 2 for
Azure AD administrative tasks such as user management, domain
management and for configuring single sign-on.
根据您在这里的具体需要,这两个选项之一应该适合您。如果这有帮助,请告诉我!
我在注册 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;
}
我就此写了一篇我认为很棒的教程:
Azure AD Authentication with PowerShell and ADAL
With these scripts, you can get authentication and REST API calls done with as little as 13 lines of PowerShell. Running the code is instant, and modifying the REST calls or even the authentication parameters takes seconds rather than minutes.
你可以在 GitHub here.
上找到我所有的脚本诀窍是您实际上可以使用 PowerShell 的 Add-Type
函数加载 ADAL 的 .NET 程序集:
# Load ADAL
Add-Type -Path ".\ADAL\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"\
从那里开始,调用所有 ADAL 函数就像在标准 C# 应用程序中一样简单。
话虽这么说,您可能想要了解的另一件事是官方 AAD PowerShell 模块:
Azure Active Directory PowerShell Version 2
You can use the Azure Active Directory PowerShell Module Version 2 for Azure AD administrative tasks such as user management, domain management and for configuring single sign-on.
根据您在这里的具体需要,这两个选项之一应该适合您。如果这有帮助,请告诉我!