gpt4 book ai didi

c# - 在 C# 中从 Azure 的 keyVault 获取 secret

转载 作者:行者123 更新时间:2023-12-03 02:56:19 27 4
gpt4 key购买 nike

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

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

获取 token 方法:

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服务

最佳答案

Is there a way I can remove from config file and fetch from anywhere else. Or is there any possible good solution to my problem.

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

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

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

这样就不需要存储相关信息,但是在运行服务之前需要先使用azure cli登录azure。 AzureServiceTokenProvider 类将 token 缓存在内存中。更多详细信息请引用authenticate to custom services .

关于c# - 在 C# 中从 Azure 的 keyVault 获取 secret ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52584954/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com