【问题标题】:Microsoft.IdentityModel.Clients.ActiveDirectory not loading on Azure DevOps PowerShell Release PipelineMicrosoft.IdentityModel.Clients.ActiveDirectory 未在 Azure DevOps PowerShell 发布管道上加载
【发布时间】:2020-12-21 10:09:05
【问题描述】:

最近,我在尝试创建 ClientCredential 和/或 ClientAssertion 时遇到了一些 Azure DevOps PowerShell 问题。我有以下代码,过去用于根据以下变量创建 ClientCredential:

  1. 租户 ID

  2. 客户 ID (SPN)

  3. 密码(SPN 密码)

    $ResourceUrl = "https://database.windows.net/"

    $AuthorityUrl = "https://login.microsoftonline.com/$($TenantId)"

    $objClientCredential = [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]::new($ClientId, $Password) $objAuthenticationContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new($AuthorityUrl) $objAuthenticationResult = $objAuthenticationContext.AcquireTokenAsync($ResourceUrl, $objClientCredential)

但最近这段代码停止工作。 AzureAD 模块似乎未正确加载。这只发生在 Azure DevOps Powershell 上,在我的机器上运行良好(我使用的是 PowerShell 7.1) 到目前为止,我尝试了以下操作:

  1. 如上图运行代码 **中断:**$objClientCredential = [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]::new($ClientId, $Password) 错误:找不到类型 [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]
  2. 在运行代码之前安装 AzureAD 安装-模块-名称 AzureAD -Force 导入模块-名称 AzureAD **中断:**$objClientCredential = [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]::new($ClientId, $Password) 错误:找不到类型 [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]
  3. 从我的机器导入 dll Microsoft.IdentityModel.Clients.ActiveDirectory.dll 添加类型-路径“.\libraries\Microsoft.IdentityModel.Clients.ActiveDirectory.dll 不会中断,但未创建 ClientCredential,它返回为 null

有没有人遇到过类似的问题?你知道我该怎么开这个吗?

【问题讨论】:

    标签: azure-devops azure-powershell


    【解决方案1】:

    如果您在本地机器上运行脚本。您可以检查程序集的安装位置并手动导入 .dll 文件。我在本地机器上进行了测试。当我只导入 azureAd 模块时它工作正常:

    Install-Module AzureAD -Force 
    Import-Module -Name AzureAD
    

    如果您在 azure 管道中运行脚本。当您在 azure powershell task 中使用 Install-Module -Name AzureAD -Force 安装 AzureAD 模块时。从构建日志中可以看到,AzureAD 模块安装在文件夹C:\Program Files\WindowsPowerShell\Modules\AzureAD\2.0.2.128

    从日志中,我们可以看到程序集Microsoft.IdentityModel.Clients.ActiveDirectory.dll 没有自动加载。

    因此您可以从模块 Azure AD 安装文件夹手动加载它:见下文:

    Install-Module AzureAD -Force 
    Import-Module -Name AzureAD
    Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\AzureAD\2.0.2.128\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
        
    $TenantId= "..."
    
    $ClientID ="..."
    
    $Password = "..."
    $ResourceUrl = "https://database.windows.net/"
    
    $AuthorityUrl = "https://login.microsoftonline.com/$($TenantId)"
    ...
    $objAuthenticationResult = $objAuthenticationContext.AcquireTokenAsync($ResourceUrl, $objClientCredential).Result
    

    查看下面的结果。

    【讨论】:

    • 嗨,我尝试了您的解决方案 Install-Module AzureAD -Force、Import-Module AzureAD Add-Type xxxxxx,但我得到的结果与我使用机器上的 dll 时相同。该代码在创建 ClientCredential 时不会引发异常,但会返回 null
    • @delucaezequiel 我检查了你的代码,发现你使用了异步方法AcquireTokenAsync。异步方法没有立即返回结果。您需要通过撤销 Result 属性来获得结果。 $objAuthenticationResult = $objAuthenticationContext.AcquireTokenAsync($ResourceUrl, $objClientCredential).Result。见上面的更新
    • AcquireTokenAsync 无法正确执行,因为 ClientCredentials 为空。在 $objAuthenticationContext.AcquireTokenAsync($ResourceUrl, $objClientCredential) 之后,我使用 $objAuthenticationContext.Result.AccessToken 返回访问令牌,但正如我所说,由于 ClientCredentials 为 null,它永远不会正确执行
    • @delucaezequiel 您是否检查了 $ClientId、$Password 是否已成功传递给 new() 方法?
    • 是的,我做到了。它们以所需的值到达。我在执行之前在控制台中打印它们并复制到文本纯文件中以检查是否错误地添加了空格。
    猜你喜欢
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多