【问题标题】:Is it possible to authenticate with an Azure Key Vault using a User Principle via a c# Winforms application?是否可以通过 c# Winforms 应用程序使用用户原则对 Azure Key Vault 进行身份验证?
【发布时间】:2020-10-05 09:43:09
【问题描述】:

我可以使用企业应用程序轻松地对 Azure KeyVault 进行身份验证,但是这在部署的环境中似乎不安全,因为我需要在应用程序本身中存储 ClientID 和 ClientSecret,我希望用户在使用 AAD 登录名的应用程序,然后使用该登录名对 KeyVault 进行身份验证,以确保任何连接的用户都获得授权。

这可能吗?

谢谢。

【问题讨论】:

    标签: c# winforms azure-keyvault


    【解决方案1】:

    是的,这在技术上可以使用 InteractiveBrowserCredentialAzure.Identity SDK。但是,通过这样做,您需要授予用户/组所需的权限(例如秘密获取等)。因此,如果您对此感到满意,请继续阅读。此外,您只需在桌面应用程序中维护 AAD 应用程序的 TenantID 和 ClientID。

    1. 在您的 AAD 租户中创建本机客户端应用类型的 AAD 应用,回复 URL 为“http://localhost”。请注意创建的应用程序的客户端 ID,您需要在代码中使用此 ID 进行用户身份验证。
    2. 将“Azure Key Vault”API 委派权限添加到上述创建的应用程序并授予管理员同意。
    3. 从访问策略向所需的 AAD 用户或组授予所需的 Keyvault 权限(如 Secret Get 等)。
    4. 在您的应用程序中添加 nuget 包:Azure.Security.KeyVault.SecretsAzure.Identity
    5. 在适当的地方添加以下代码,例如表单加载或您需要执行密钥保管库操作(例如检索机密等)的地方。
                // 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>");
    

    瞧,就是这样。它将打开一个浏览器窗口以提示用户登录并继续使用来自用户原则的身份验证令牌。

    【讨论】:

      猜你喜欢
      • 2018-04-06
      • 2020-07-22
      • 2020-08-21
      • 2019-09-12
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 2023-01-10
      • 2022-08-13
      相关资源
      最近更新 更多