Azure App Configuration - How to set a secret

时间:2019-03-19 15:03:08

标签: azure azure-configuration

I follow this guide to set up Azure App Configuration.

But I think I misunderstand this line:

dotnet user-secrets set ConnectionStrings:AppConfig "Endpoint=<your_endpoint>;Id=<your_id>;Secret=<your_secret>"

Or rather; what is what...

  • your_endpoint = I set my Primary Key Connection String (copied from the App Configuration resource in Azure)
  • your_id = The guid is set as UserSecretsId (a few step up in the guide)
  • your_secret = the key to the secret (ConnectionStrings:AppConfig in guide)

But then my program always crash at:

config.AddAzureAppConfiguration(settings["ConnectionStrings:AppConfig"]);

with exception:

System.FormatException HResult=0x80131537 Message=The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. Source=System.Private.CoreLib StackTrace: at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength) at System.Convert.FromBase64String(String s) at Microsoft.Azure.AppConfiguration.Azconfig.AzconfigClient..ctor(String connectionString, HttpMessageHandler httpMessageHandler, Boolean disposeHandler) at Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureAppConfigurationSource.Build(IConfigurationBuilder builder) at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors) at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at Persistance.API.Program.Main(String[] args) in C:\Repos\experiment\experiment\Program.cs:line 19

when executing this code block (from guide):

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        var settings = config.Build();
        config.AddAzureAppConfiguration(settings["ConnectionStrings:AppConfig"]);
    })
    .UseStartup<Startup>();

Right-click on project -> Manage User Secrets I have this json file:

 {
    "Movies:ServiceApiKey": "12345",
    "ConnectionStrings:AppConfig": "Endpoint=<Primary Key Connection String copied value from azure resource>;Id=<the UserSecretsId tag in csproj file>;Secret=<the Key value in App Configuration resource>"
 }

Worth noting; my primary key connection string contains chars ';', '-', '+' and '=' but those shouldn't be illeagal characters...

2 个答案:

答案 0 :(得分:1)

此处的关键不是解释连接字符串的各段,而是将其当作不透明的标记。连接字符串可以直接从门户网站复制,也可以通过azure CLI获取

az extension add -n appconfig
az appconfig credential list -n <your-app-configuration-name>

一旦有了连接字符串,就应使用准确,完整的值来调用AddAzureAppConfiguration。连接字符串的“秘密”部分很可能以“ =”字符结尾,我怀疑它没有被复制。

答案 1 :(得分:0)

我想到两件事:

  1. 确保您从与csproj文件存在的目录相同的目录中运行add secrets命令。
  2. 用户机密仅在本地执行时起作用。因此,如果您在远程某个地方执行,它将失败。 CreateDefaultBuilder calls AddUserSecrets when the EnvironmentName is Development:

如果在.net核心中使用此功能,则可以在配置内部的launch.json中指定以下内容:

"env": {
    "ASPNETCORE_ENVIRONMENT": "Development"
},