是否可以通过 c# Winforms 应用程序使用用户原则对 Azure Key Vault 进行身份验证?
Is it possible to authenticate with an Azure Key Vault using a User Principle via a c# Winforms application?
我可以使用企业应用程序轻松地针对 Azure KeyVault 进行身份验证,但是这在已部署的环境中似乎不安全,因为我需要在应用程序本身中存储 ClientID 和 ClientSecret,我更愿意让用户进行身份验证在应用程序中使用 AAD 登录,然后使用该登录对 KeyVault 进行身份验证,以确保所有连接的用户都获得授权。
这可能吗?
谢谢。
是的,使用 Azure.Identity
SDK 的 InteractiveBrowserCredential
在技术上是可行的。但是,通过这样做,您需要给予 User/Group 所需的权限(例如秘密获取等)。因此,如果您对此表示满意,请继续阅读。此外,您只需要在桌面应用程序中维护 AAD 应用程序的 TenantID 和 ClientID。
- 在您的 AAD 租户中创建本地客户端应用程序类型的 AAD 应用程序,回复 URL“http://localhost”。请注意创建的应用程序的客户端 ID,您将需要在代码中使用它来进行用户身份验证。
- 向上面创建的应用程序添加“Azure Key Vault”API 委派权限并授予管理员同意。
- 从访问策略向所需的 AAD 用户或组授予所需的 Keyvault 权限(如 Secret Get 等)。
- 在您的应用程序中添加 nuget 包:
Azure.Security.KeyVault.Secrets
和 Azure.Identity
- 在合适的地方添加以下代码,例如表单加载或需要进行密钥保管库操作(例如检索机密等)的地方。
// Create a new secret client using the Interactive credential from Azure.Identity
// This will prompt the user to login .
var client = new SecretClient(vaultUri: new Uri("https://<keyvault-name>.vault.azure.net/"),
credential: new InteractiveBrowserCredential("<tenant-id>", "<client-id>"));
// Retrieve a secret using the secret client.
var secret = await client.GetSecretAsync("<secret-name>");
瞧,就是这样。它将打开浏览器 window 以提示用户登录并从用户原则继续使用身份验证令牌。
我可以使用企业应用程序轻松地针对 Azure KeyVault 进行身份验证,但是这在已部署的环境中似乎不安全,因为我需要在应用程序本身中存储 ClientID 和 ClientSecret,我更愿意让用户进行身份验证在应用程序中使用 AAD 登录,然后使用该登录对 KeyVault 进行身份验证,以确保所有连接的用户都获得授权。
这可能吗?
谢谢。
是的,使用 Azure.Identity
SDK 的 InteractiveBrowserCredential
在技术上是可行的。但是,通过这样做,您需要给予 User/Group 所需的权限(例如秘密获取等)。因此,如果您对此表示满意,请继续阅读。此外,您只需要在桌面应用程序中维护 AAD 应用程序的 TenantID 和 ClientID。
- 在您的 AAD 租户中创建本地客户端应用程序类型的 AAD 应用程序,回复 URL“http://localhost”。请注意创建的应用程序的客户端 ID,您将需要在代码中使用它来进行用户身份验证。
- 向上面创建的应用程序添加“Azure Key Vault”API 委派权限并授予管理员同意。
- 从访问策略向所需的 AAD 用户或组授予所需的 Keyvault 权限(如 Secret Get 等)。
- 在您的应用程序中添加 nuget 包:
Azure.Security.KeyVault.Secrets
和Azure.Identity
- 在合适的地方添加以下代码,例如表单加载或需要进行密钥保管库操作(例如检索机密等)的地方。
// Create a new secret client using the Interactive credential from Azure.Identity
// This will prompt the user to login .
var client = new SecretClient(vaultUri: new Uri("https://<keyvault-name>.vault.azure.net/"),
credential: new InteractiveBrowserCredential("<tenant-id>", "<client-id>"));
// Retrieve a secret using the secret client.
var secret = await client.GetSecretAsync("<secret-name>");
瞧,就是这样。它将打开浏览器 window 以提示用户登录并从用户原则继续使用身份验证令牌。