【问题标题】:How to get a usersecret in an Azure Function?如何在 Azure 函数中获取用户密码?
【发布时间】:2021-11-20 07:59:40
【问题描述】:

我需要从 usersecrets 中获取连接字符串。我尝试过这样:

CosmosClient cosmos = new CosmosClient("CosmosConnectionString", new CosmosClientOptions
        {
            ConnectionMode = ConnectionMode.Gateway
        });

但这不起作用,因为 CosmosClient 只能理解文字字符串。我尝试用 usersecrets 注入 Azure Functions,为此我创建了一个启动类:

    [assembly: FunctionsStartup(typeof(Startup))]
namespace AltProject
{
    public class Startup : FunctionsStartup
    {
        // override configure method
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var config = new ConfigurationBuilder()
               .SetBasePath(Environment.CurrentDirectory)
               .AddJsonFile("appsettings.json", true)
               .AddUserSecrets(Assembly.GetExecutingAssembly(), false)
               .AddEnvironmentVariables()
               .Build();
           
            builder.Services.AddSingleton<IConfiguration>(config);
            foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
                Console.WriteLine("  {0} = {1}", de.Key, de.Value);            // register your other services
        }
    }

}

然后我尝试在 CosmosClient 中以这种方式获取连接字符串:

      CosmosClient cosmos = new CosmosClient(System.Environment.GetEnvironmentVariable("CosmosConnectionString", EnvironmentVariableTarget.Process), new CosmosClientOptions
        {
            ConnectionMode = ConnectionMode.Gateway
        });

但这没有给我任何结果,似乎环境值没有注入用户机密。有没有办法让这个工作?

【问题讨论】:

    标签: c# azure-functions azure-cosmosdb


    【解决方案1】:

    完整示例位于:https://github.com/Azure/azure-cosmos-dotnet-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/AzureFunctions

    您应该能够毫无问题地读取 Startup 中的配置。假设您的 appsettings.json 包含一个名为 CosmosConnectionString 的密钥,或者您通过 Functions App Settings 添加它:

    [assembly: FunctionsStartup(typeof(AltProject.Startup))]
    namespace AltProject
    {
        private static readonly IConfigurationRoot configuration = new ConfigurationBuilder()
                    .SetBasePath(Environment.CurrentDirectory)
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddEnvironmentVariables()
                    .Build();
    
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton((s) => {
                string connectionString = configuration["CosmosConnectionString"];
                if (string.IsNullOrEmpty(connectionString))
                {
                    throw new ArgumentNullException("CosmosConnectionString empty or not set at appsettings.json file or your Azure Functions Settings.");
                }
    
                return new CosmosClient(connectionString, new CosmosClientOptions
                {
                    ConnectionMode = ConnectionMode.Gateway
                });
            });
        }
    }
    

    然后您可以在您的函数中注入CosmosClient

    public class MyFunction
    {
        private CosmosClient cosmosClient;
        public MyFunction(CosmosClient cosmosClient)
        {
            this.cosmosClient = cosmosClient;
        }
    
    
        [FunctionName("CosmosClient")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            // use this.cosmosClient
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多