cob*_*nks 3 azure azure-keyvault
在我的公司,我们还不能进入 .net core。我正在尝试研究如何最好地使用 azure 密钥保管库来存储我们的 api 应用程序服务的配置项。
我有一个带有 global.asax 文件的简单 webapi 项目:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.WebHost;
using System.Web.Routing;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureKeyVault;
namespace kv.api
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
LoadAzureKeyVaultSettings();
}
protected void LoadAzureKeyVaultSettings()
{
var tokenProvider = new AzureServiceTokenProvider("RunAs=CurrentUser;");
var kvClient = new KeyVaultClient((authority, resource, scope) => tokenProvider.KeyVaultTokenCallback(authority, resource, scope));
var builder = new ConfigurationBuilder()
.AddAzureKeyVault("https://mykvurihere.vault.azure.net/", kvClient, new DefaultKeyVaultSecretManager());
builder.Build();
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在这里有一个简单的 webapi 端点:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using kv.api.Models;
namespace kv.api.Controllers
{
public class SettingsController : ApiController
{
/// <summary>
/// Method that returns all the keys out of the Configuration Manager's App Settings. Can use this endpoint to test KeyVault integrations.
/// </summary>
/// <returns>List of Settings</returns>
public IEnumerable<Setting> GetAllSettings()
{
var settings = ConfigurationManager.AppSettings.AllKeys
.Select(key => new Setting()
{
Key = key,
Value = ConfigurationManager.AppSettings[key]
})
.ToList();
return settings;
}
}
}
Run Code Online (Sandbox Code Playgroud)
它编译,我没有得到运行时异常,但是这个端点没有从密钥库中产生我的配置(我确实得到了在我的 web.config 中定义的 appSettings)。我在这里缺少什么?
--- 更新 azure 门户中报告的 Key Vault 指标似乎显示我的应用已成功检索机密,但它们并未添加到应用的 AppSettings...
谢谢!
我已经尽力解决了这个问题,所以我决定写一篇关于它的长篇博文,你可以在这里找到。
简而言之,在我看来,集成 Key Vault 配置生成器的最佳方法不是通过 .NET 代码,而只需将 Key Vault 添加为连接服务,然后在您的 Web.config 中进行设置,如下所示:
<configuration>
<configSections>
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
</configSections>
<configBuilders>
<builders>
<add name="AzureKeyVault" vaultName="your vault's name" type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=1.0.0.0, Culture=neutral" />
</builders>
</configBuilders>
<appSettings configBuilders="AzureKeyVault">
<add key="MyValue" value="Value from Web.config" />
</appSettings>
...
</configuration>
Run Code Online (Sandbox Code Playgroud)
然后,如果您在 Key Vault 和应用程序之间正确设置了身份验证,向 Key Vault 添加名称为“MyValue”的机密,它将在运行时被替换,您将能够从 Key Vault 访问您的密钥像这样的应用程序:
ConfigurationManager.AppSettings["MyValue"]
Run Code Online (Sandbox Code Playgroud)
我找到了一个解决方案,但它看起来真的很奇怪......将其发布到此处以获得反馈。我最终做的是手动设置 ConfigurationManager.AppSettings 集合中的键/值,如下所示:
using System.Configuration;
using System.Web.Http;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureKeyVault;
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
namespace kv.api
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
LoadAzureKeyVaultSettings();
}
protected void LoadAzureKeyVaultSettings()
{
var tokenProvider = new AzureServiceTokenProvider(ConfigurationManager.AppSettings["AzureServiceTokenProviderConnectionString"]);
var kvClient = new KeyVaultClient(
new KeyVaultClient.AuthenticationCallback(tokenProvider.KeyVaultTokenCallback));
var builder = new ConfigurationBuilder()
.AddAzureKeyVault("https://mykvurihere.vault.azure.net/", kvClient,
new DefaultKeyVaultSecretManager());
var config = builder.Build();
foreach (var keyValuePair in config.AsEnumerable())
{
ConfigurationManager.AppSettings.Set(keyValuePair.Key, keyValuePair.Value);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2901 次 |
| 最近记录: |