【问题标题】:MSAL library with powershell for azure devops token cache issue用于 azure devops 令牌缓存问题的带有 powershell 的 MSAL 库
【发布时间】:2021-11-03 13:40:16
【问题描述】:

我正在使用 MSAL 库来获取 azure devops 的令牌。 我正在编写 powershell 脚本。

问题是,如果我关闭当前的 powershell7 会话并再次重新打开,它会要求选择/重新登录。它不会自动静默使用 get token。 它在同一个会话中工作正常,它正在正确地静默获取令牌,如果我关闭并重新打开 powershell 窗口,就会出现问题。 我怎样才能在下一个会话中缓存?


[string[]] $Scopes = "$adoResourceId/.default";
[Microsoft.Identity.Client.IPublicClientApplication] $app = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::Create($ClientId).Build();

[ContextHelper]::Account = $app.GetAccountsAsync().GetAwaiter().GetResult() | Select-Object -First 1
$tokenSource = New-Object System.Threading.CancellationTokenSource
$taskAuthenticationResult = $null
try {
    $AquireTokenParameters = $app.AcquireTokenSilent($Scopes, [ContextHelper]::Account)
    $taskAuthenticationResult = $AquireTokenParameters.ExecuteAsync($tokenSource.Token)
    if ($taskAuthenticationResult.exception.message -like "*errors occurred*") {
        $AquireTokenParameters = $app.AcquireTokenInteractive($Scopes)
        $taskAuthenticationResult = $AquireTokenParameters.ExecuteAsync($tokenSource.Token)
    }
}
catch {
    $AquireTokenParameters = $app.AcquireTokenInteractive($Scopes)
    $taskAuthenticationResult = $AquireTokenParameters.ExecuteAsync($tokenSource.Token)
}
if ($taskAuthenticationResult.Result) {
    $result = $taskAuthenticationResult.Result;
}

谢谢

【问题讨论】:

    标签: powershell authentication caching azure-devops msal


    【解决方案1】:

    恐怕你不能,凭据不会缓存在本地,打开新的PowerShell会话时需要交互式登录。

    其实,Azure DevOps 的token不需要这么复杂的获取方式,直接使用Azure PowerShell即可,请按照以下步骤操作。

    1. 按照doc 安装Az powershell 模块。

    2. 使用Connect-AzAccount 登录您的用户帐户。

    3. 使用Get-AzAccessToken获取Azure DevOps的token,499b84ac-1321-427f-aa17-267ca6975798是Azure DevOps的资源id。

      Get-AzAccessToken -ResourceUrl "499b84ac-1321-427f-aa17-267ca6975798"

    4. 登录 Azure PowerShell 后,它将用户上下文作为 AzureRmContext.json 文件存储在本地路径 ~/.azure 中。因此,如果您关闭 powershell 会话并使用 powershell 7 打开一个新会话,则无需再次登录。您可以使用Get-AzContext 进行快速检查,然后再次获取令牌,它在我这边工作正常。

    【讨论】:

    • 感谢乔伊的回答。 Get-AzContext 仅在用户连接广告时才有效。如果用户没有连接到广告怎么办?
    • @ArvindSingh 是的,你是对的,因为 powershell 本质上在请求 url 中使用 /organizations 端点。如果用户帐户不在 AAD 中,您可以将其邀请到 AAD,请关注此doc
    • @ArvindSingh 接受邀请后并在获取令牌之前,请确保使用正确的租户使用Connect-AzAccount -Tenant <AAD tenant-id> 登录或在登录后使用Set-AzContext -Tenant <AAD tenant-id>。登录的AAD应该是which the Azure devops Org backed in,否则token不起作用。
    • 嗨@Joy 再次感谢您的宝贵意见。我正在使用 ADAL 做类似的事情,它在 PS5 上运行良好,即使在关闭 PS ISE 并再次重新打开后,它也不会再次要求重新选择/重新登录,它会自动使用上次使用的登录身份。代码:``` $PromptBehavior = [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Auto;$PlatformParameters = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters -ArgumentList $PromptBehavior; $result = $ctx.AcquireTokenAsync($adoResourceId, $clientId, [Uri]::new($replyUri),$PlatformParameters).Result; ```
    • 我无法邀请和添加,如果用户帐户不在 AAD 中。无法使用 Connect-AzAccount 和 azcontext
    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2021-10-26
    • 2021-12-06
    • 1970-01-01
    • 2021-04-29
    • 2021-07-14
    • 1970-01-01
    相关资源
    最近更新 更多