【发布时间】: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