powershell - 使用 Powershell 获取 Azure AD token (jwt)

标签 powershell azure azure-active-directory

我正在尝试使用 Powershell 使用用户名/密码身份验证从 AAD 获取 jwt token 。

我正在编写一个 powershell 脚本,它将使用不记名 token 调用 API。如果我从使用 API 的 SPA 复制并粘贴 token ,我所拥有的工作。我正在寻找一种从我的 powershell 中检索 token 的方法。

这看起来很有希望:https://github.com/Azure-Samples/active-directory-dotnet-native-headless/blob/master/TodoListClient/Program.cs

我觉得我在试图创建一个“UserPasswordCredential”对象时用头撞墙。任何关于我如何做到这一点的线索都会非常有帮助。

我有 Add-Type-ed:
- Microsoft.IdentityModel.Clients.ActiveDirectory.dll
- Microsoft.IdentityModel.Clients.ActiveDirectory.platform.dll(什么都不添加?)
- Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll
- Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll

'UserPasswordCredential' 的文档页面:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.identitymodel.clients.activedirectory.userpasswordcredential
它应该在前两个 dll 之一中

在“约束和限制”下,这让我认为它实际上可能无法从 powershell 中实现:
http://www.cloudidentity.com/blog/2014/07/08/using-adal-net-to-authenticate-users-via-usernamepassword/

查看下面的代码,第一个获取 token 成功,第二个失败 - 可能/可能是因为 $cred 是 UserCredential 而不是 UserPasswordCredential。

可以用powershell做到这一点吗?

最后,在完全不同的轨道上,我如何找到我的应用程序需要的 redirectUri 和 resourceAppIdURI 的值?当我在 AAD 控制台和浏览器中查看我的企业应用程序时,我可以找到 AppId(我可以将其用作 $clientId)。
我不确定 redirectUri 对我来说是否绝对必要,因为我真正想要的是 token ,但我可以很好地猜测它应该是什么。
当我尝试使用我的应用程序详细信息调用第一个 AquireToken 方法(没有 $cred)时,它失败并显示以下消息:

Exception calling "AcquireToken" with "4" argument(s): "AADSTS50001: The application named https://myappwithapi/Login was not found in the tenant named me.onmicrosoft.com.

我是否可以通过查看我的 azure 门户找到 resourceAppIdURI 的所需值?
' https://myappwithapi/Login ' 来自我的 azure 门户 > 企业应用程序 > [应用程序' > 属性 > HomepageUrl

代码:
#setup
$TenantName = "mme.onmicrosoft.com"
$clientId = "1950a258-227b-4e31-a9cf-717495945fc2" # Microsoft
$clientId = "03faf8db-..........................." #

$username = "me@me.onmicrosoft.com"
$password = Read-Host -AsSecureString -Prompt "Enter Password"


# add dlls
$adal = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"
$adalplatform = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.platform.dll"
[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
[System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null
[System.Reflection.Assembly]::LoadFrom($adalplatform) | Out-Null



#prep request
$redirectUri = "urn:ietf:wg:oauth:2.0:oob" # Microsoft
$resourceAppIdURI = "https://graph.windows.net"
$authority = "https://login.windows.net/$TenantName"
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority


# Get Token prompting for creds
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Always")
$authResult


# Get the cred
$cred = New-Object -TypeName 'Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential' -ArgumentList $username, $password
#$cred = New-Object -TypeName 'Microsoft.IdentityModel.Clients.ActiveDirectory.UserPassCredential' -ArgumentList $username, $password
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $cred)

$authResult

最佳答案

这篇文章有更多的一个问题。

您的基本用例“使用 Powershell 获取 Azure AD token (jwt)”是一个常见用例,并且有几个示例和预构建示例可供利用。例如:

https://github.com/pcgeek86/AzureADToken
一个 PowerShell 模块,可让您从 Azure Active Directory (AAD) 获取 JSON Web token (JWT)。

https://gallery.technet.microsoft.com/Get-Azure-AD-Bearer-Token-37f3be03
此脚本获取可用于使用 Postman 等工具对 Azure 资源管理器 API 进行身份验证的不记名 token 。它使用随 Azure SDK 安装的 Active Directory 身份验证库。

看看这两个资源是否解决了您的使用基线用例。

至于这个……
“我是否可以通过查看我的 azure 门户找到 resourceAppIdURI 的所需值?”

您可以通过远程 PowerShell 登录到 AzureAD 来执行此操作。安装 AAD PowerShell 模块。
https://docs.microsoft.com/en-us/powershell/azure/overview?view=azurermps-5.1.1
https://msdn.microsoft.com/en-us/library/dn135248(v=nav.70).aspx

下载并安装 MSOL。使用 MSOL 登录
https://www.microsoft.com/en-US/download/details.aspx?id=39267
Microsoft Online Services 登录助手提供最终用户登录功能

并使用内置 cmdlet 从您的组织设置中提取您的信息,或者点击 MSGraph API 和查询。
https://docs.microsoft.com/en-us/powershell/azure/active-directory/install-adv2?view=azureadps-2.0
您可以将 Azure Active Directory PowerShell 模块版本用于 Azure AD 管理任务

至于这个:
“如何找到我的应用程序需要的 redirectUri 和 resourceAppIdURI 的值?”
这位于门户的应用注册部分。开发人员团队提供的是 redir uri 而不是 Azure。它是注册过程的一部分,其他所有内容均由 Azure App Reg 过程生成。

应用程序注册过程在这里,当然你是其他人必须在 AzureAD 中注册这个应用程序,因此可以随时检索它。:
https://blogs.msdn.microsoft.com/onenotedev/2015/04/30/register-your-application-in-azure-ad

任何注册的应用程序及其详细信息都可以使用...检索

Get-AzureADApplication
Get-AzureADApplication | Select -Property *
(Get-AzureADApplication).ReplyUrls
Get-AzureADApplication | Select -Property AppID, DisplayName,ReplyUrls

https://docs.microsoft.com/en-us/powershell/module/azuread/get-azureadapplication?view=azureadps-2.0

关于powershell - 使用 Powershell 获取 Azure AD token (jwt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48070645/

相关文章:

windows - 如何在 Windows 上使用 PowerShell 脚本连接和断开蓝牙设备?

azure - 我们可以在没有管理员权限的情况下在 VSCode 中本地运行 azure 函数吗

powershell - 通过 ARM PowerShell 设置粘性应用程序/连接字符串

c# - Azure Active Directory 回复 URL 未按预期工作

Azure AD 身份验证重定向循环

powershell - 高级语法

powershell - 为 Posh-SSH New-SSHSession 提供私钥 passhprase(密码)

powershell - 如何在 PowerShell 中标准化路径?

Azure Key Vault 事件 - 是否可以订阅事件网格主题(不是系统事件网格主题)?

java - 使用 Java 访问受 Azure Active Directory 保护的 Web 服务