c# - 将 Azure Key Vault key 传递给 Blazor 应用程序中的 Services.Configure

标签 c# mongodb azure dependency-injection blazor

我在将 azure KeyVault key 传递到我的数据库设置配置时遇到困难。

我的代码如下: 程序.cs

var keyVaultEndPoint = new Uri(builder.Configuration["VaultKey"]);
var secretClient = new SecretClient(keyVaultEndPoint, new DefaultAzureCredential());

KeyVaultSecret kvs = secretClient.GetSecret(builder.Configuration["SecretName"]);

var database = builder.Configuration.GetSection("Database");

builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor()
    .AddMicrosoftIdentityConsentHandler();
    
builder.Services.Configure<DatabaseSettings>(
builder.Configuration.GetSection("Database"));

builder.Services.AddSingleton<TodoService>();
builder.Services.AddSingleton<RecipesService>();
builder.Services.AddSingleton<SpecialDatesService>();

appsettings.Json

"Database": {
    "ConnectionString": null,
    "DatabaseName": "Personal",
    "TodoCollectionName": "todo",
    "RecipesCollectionName": "recipes",
    "SpecialDatesCollectionName": "specialdates"
  },

我在 kvs.Value 处获得了正确的连接字符串,但我不知道如何在 DatabseSettings 配置中设置它。

有什么想法吗?

我的目标是以某种方式将 keyvault secret 注入(inject)到我的服务正在使用的 DatabaseSettings 对象中。

最佳答案

builder.Services.Configure(options => { options.ConnectionString = kvs.Value; builder.Configuration.GetSection("Database").Bind(options); });

关于c# - 将 Azure Key Vault key 传递给 Blazor 应用程序中的 Services.Configure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76261812/

相关文章:

c# - 默认等效于 DBNull.Value

c# - 将具有指数表示法的数字从字符串转换为 double 或十进制

mongodb - 使用Grails将列表保存在mongodb中

eclipse - Microsoft Azure 部署错误 40%

c# - Monodevelop gtk-sharp 3.0

c# - Visual Studio 2010 中的 MVVM?

c# - MongoDB C# 驱动程序 - 如何在 .NET 中对加入的集合强制执行投影?

java - 找不到类 org.springframework.data.mongodb.core.query.GeoCommand 的编解码器

javascript - 调试 MobileServiceClient 请求

Azure ML 推理管道部署授权 token 错误