桌面应用程序如何使用Azure AD读取KeyVault机密?
我能够获取MSAL令牌,但将其交给KeyVaultClient总是会导致:
Microsoft.Azure.KeyVault.Models.KeyVaultErrorException:操作返回了无效的状态码“未经授权”
我什至不确定KeyVault是否支持这种令牌,但是在我的Google搜索中,我看到了使用较旧的ADAL令牌的示例。
我的KeyVault对我的Azure AD帐户和我所属的组都有访问策略。
我从MSAL获得的JWT令牌的有效载荷具有以下范围:
"scp": "User.Read profile openid email"
这是一个mcve:
public partial class MainWindow : Window
{
private static string ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
public static PublicClientApplication PublicClientApp = new PublicClientApplication(ClientId);
public MainWindow()
{
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var secret = await GetKeyVaultSecret("TestSecret");
}
private async Task<string> GetKeyVaultSecret(string secretKey)
{
KeyVaultClient kvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(AcquireMSALToken));
try
{
var secretBundle =
await kvClient.GetSecretAsync("https://xxxxx.vault.azure.net/",
secretKey, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
return secretBundle.Value;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
private static async Task<string> AcquireMSALToken(string authority, string resource, string scope)
{
string[] _scopes = new string[] { "user.read" };
AuthenticationResult authResult = null;
var app = PublicClientApp;
var accounts = await app.GetAccountsAsync();
try
{
authResult = await app.AcquireTokenSilentAsync(_scopes, accounts.FirstOrDefault());
return authResult.AccessToken;
}
catch (MsalUiRequiredException)
{
try
{
authResult = await PublicClientApp.AcquireTokenAsync(_scopes);
return authResult.AccessToken;
}
catch (MsalException) { throw; }
}
catch (Exception) { throw; }
}
}
更新
仅供参考,这是可行的,但我认为这不是AzureServiceTokenProvider用于基于文档的目的。这使我相信MSAL令牌与KeyVault不兼容。我的感觉是它仅用于MS Graph调用。
using Microsoft.Azure.Services.AppAuthentication;
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
var kvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secretBundle = await kvClient.GetSecretAsync("https://xxxxxxxx.vault.azure.net/",
"TestSecret", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
return secretBundle.Value;
答案 0 :(得分:2)