繁体   English   中英

使用Powershell获取Azure AD令牌(jwt)

[英]Using Powershell to get Azure AD Token (jwt)

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

我正在编写一个powershell脚本,它将使用持票令牌调用API。 如果我从使用API​​的SPA复制并粘贴令牌,我的工作原理是什么。 我正在寻找一种从我的PowerShell中检索令牌的方法。

这看起来很有希望: 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 /

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

有可能用powershell做到这一点吗?


最后,在一个完全不同的轨道上,如何找到我的应用程序需要的redirectUri和resourceAppIdURI的值? 当我查看AAD控制台和浏览器到我的企业应用程序时,我可以找到AppId(我可以将其用作$ clientId)。
我不确定redirectUri对我来说是非常必要的,因为我真正想要的只是令牌,但我可以很好地猜测它应该是什么。 当我尝试使用我的应用程序详细信息调用第一个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的require值吗?
' https:// myappwithapi / Login '来自我的天蓝色门户网站>企业应用> [app'> properties> 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令牌(jwt)”是一个常见的用例,并且有几个示例和预先构建的示例可供使用。 例如:

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

https://gallery.technet.microsoft.com/Get-Azure-AD-Bearer-Token-37f3be03此脚本获取一个承载令牌,该令牌可用于使用Postman等工具对Azure Resource Manager API进行身份验证。 它使用随Azure SDK一起安装的Active Directory身份验证库。

查看这两个资源是否可以解析您的使用基线用例。

至于这个......“我可以通过查看我的天蓝色门户网站找到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 /注册-你的应用程序功能于Azure的广告

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM