从 Connect-AzureAd 公开连接令牌

Jus*_*ing 1 powershell azure-ad-graph-api

我正在使用AzureAd Powershell 模块进行用户管理。但是,它没有我需要的所有功能,特别是,我无法将应用程序扩展值分配给对象(尽管我可以通过创建删除和删除应用程序扩展本身[Get/New/Remove]-AzureADApplicationExtensionProperty)。

通过使用Fiddler观察 API 调用,我知道图形调用正在使用不记名令牌,并且我已直接从 Postman 手动调用图形 API,因此我知道如何使用不记名令牌(如果可以获得)。我怎样才能得到它?

Jus*_*ing 6

要获取令牌,只需使用:

$token = [Microsoft.Open.Azure.AD.CommonLibrary.AzureSession]::AccessTokens['AccessToken']

但怎么会得出这样的结论呢?

首先查找模块所在位置:

(Get-Module AzureAd).Path
C:\Program Files\WindowsPowerShell\Modules\AzureAD\2.0.1.3\Microsoft.Open.AzureAD16.Graph.PowerShell.dll
Run Code Online (Sandbox Code Playgroud)

现在让我们做两个假设。首先,令牌存储在静态类的静态成员中,其次,它可能不存储在该 dll 中,而是存储在文件夹中的任何 DLL 中。

$fileInfo = New-Object 'IO.FileInfo' (Get-Module AzureAd).Path
$moduleFolder = $fileInfo.Directory.FullName
$assemblies = [AppDomain]::CurrentDomain.GetAssemblies() | where { $_.Location -ne $null -and  $_.Location.StartsWith($moduleFolder)}
$assemblies | select -expandproperty ExportedTypes | Where { $_.IsSealed -and $_.IsAbstract } | Select Name, FullName
Run Code Online (Sandbox Code Playgroud)

顺便说一句,最后一行是因为IL 中静态类型的注释方式很奇怪。

它输出一个非常小的列表:

Name                                          FullName
----                                          --------
RestSharpExtensionMethods                     Microsoft.Open.Azure.AD.CommonLibrary.RestSharpExtensionMethods
AzureSession                                  Microsoft.Open.Azure.AD.CommonLibrary.AzureSession
DictionaryExtensions                          Microsoft.Open.Azure.AD.CommonLibrary.DictionaryExtensions
Logger                                        Microsoft.Open.Azure.AD.CommonLibrary.Logger
ImageUtils                                    Microsoft.Open.Azure.AD.CommonLibrary.Utilities.ImageUtils
SecureStringExtension                         Microsoft.Open.Azure.AD.CommonLibrary.Extensions.SecureStringExtension
AzureEnvironmentConstants                     Microsoft.Open.Azure.AD.CommonLibrary.AzureEnvironment+AzureEnvironmentConstants
TypeToOdataTypeMapping                        Microsoft.Open.AzureAD16.Client.TypeToOdataTypeMapping
JsonConvert                                   Newtonsoft.Json.JsonConvert
Extensions                                    Newtonsoft.Json.Linq.Extensions
Extensions                                    Newtonsoft.Json.Schema.Extensions
TypeToOdataTypeMapping                        Microsoft.Open.MSGraphV10.Client.TypeToOdataTypeMapping
AdalError                                     Microsoft.IdentityModel.Clients.ActiveDirectory.AdalError
AuthenticationContextIntegratedAuthExtensions Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions
AdalOption                                    Microsoft.IdentityModel.Clients.ActiveDirectory.AdalOption
MiscExtensions                                RestSharp.Extensions.MiscExtensions
ReflectionExtensions                          RestSharp.Extensions.ReflectionExtensions
ResponseExtensions                            RestSharp.Extensions.ResponseExtensions
ResponseStatusExtensions                      RestSharp.Extensions.ResponseStatusExtensions
StringExtensions                              RestSharp.Extensions.StringExtensions
XmlExtensions                                 RestSharp.Extensions.XmlExtensions
RestClientExtensions                          RestSharp.RestClientExtensions
SimpleJson                                    RestSharp.SimpleJson
Run Code Online (Sandbox Code Playgroud)

如果列表更长,我们可以通过Out-GridviewAzureSession进行管道传输,但我的注意力立即被吸引到了。之后,进行了一些 PowerShell 自动完成操作,我找到了方法[Microsoft.Open.Azure.AD.CommonLibrary.AzureSession]::AccessTokens['AccessToken']