【问题标题】:Fetching secrets from keyVault from Azure in c#在 c# 中从 Azure 中的 keyVault 获取机密
【发布时间】:2018-10-01 05:19:50
【问题描述】:

我有以下代码,它从 KeyVault 检索秘密。

var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken));
var sec = await kv.GetSecretAsync(ConfigurationManager.AppSettings["SomeURI"]);
secretValue = sec.Value ;

GetToken 方法:

async Task<string> GetToken(string authority, string resource, string scope)
{
    var authContext = new AuthenticationContext(authority);
    ClientCredential clientCred = new ClientCredential(ConfigurationManager.AppSettings["ClientId"],ConfigurationManager.AppSettings["ClientSecret"]);
    AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);    
    if (result == null)
        throw new InvalidOperationException("Failed to obtain the token");    
    return result.AccessToken;
}

在 GetToken 方法中,我从 Appconfig 获取 ClientIdClientSecret

我觉得将这些值保留在 Appconfig 中并使用它们是不安全的。有没有办法可以从配置文件中删除并从其他任何地方获取。或者我的问题有什么好的解决方案。

非常感谢任何回应!

PS:我的是用c#开发的windows服务

【问题讨论】:

    标签: c# azure azure-keyvault


    【解决方案1】:

    有没有办法可以从配置文件中删除并从其他任何地方获取。或者有什么好的办法可以解决我的问题。

    根据我的理解,您可以将相关信息存储到数据库中。并且您可以使用windows Authentication 访问数据库以获取相关信息。

    使用托管身份的另一种方法是通过Microsoft.Azure.Services.AppAuthentication

    var azureServiceTokenProvider = new AzureServiceTokenProvider();
    var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(
    azureServiceTokenProvider.KeyVaultTokenCallback));
    

    这种方式不需要存储相关信息,但是需要先使用azure cli登录到azure,然后再运行服务。 AzureServiceTokenProvider 类将令牌缓存在内存中。更多详细信息请参考authenticate to custom services

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      相关资源
      最近更新 更多