.net 需要帮助从appSettings检索此内容,而无需硬编码

6yjfywim  于 2023-03-13  发布在  .NET
关注(0)|答案(2)|浏览(146)

我需要一些帮助,从appSettings检索使用disctionary没有硬编码的值。

"HMAC": {
      "MLC": {
        "Server": {
           "PE": {
             "ApiKey": null,
             "SecretKey": null
             },
           "DNB": {
             "ApiKey": null,
             "SecretKey": null
           }
          },
        "Clients": {
             "Billing": {
                  "ApiKey": null,
                  "SecretKey": null
                 },
             "Finance": {
                  "ApiKey": null,
                  "SecretKey": null
             }
          }
      }
   },

目前,这是我如何在我的appSettings中获得apiKeys。但它没有MLC层。我不知道如何添加其他层来获得服务器或客户端密钥。

public class HMAC
    {

        public IDictionary<string, HMACSettings> Server { get; set; } = null!;
        public IDictionary<string, HMACSettings> Clients { get; set; } = null!;
        public HMACSettings? GetServer(string KeyName)
        {
            return GetValue(this.Server, KeyName);
        }
        public HMACSettings? GetClients(string KeyName)
        {
            return GetValue(this.Clients, KeyName);
        }
        public HMACSettings? GetValue(IDictionary<string, HMACSettings> HmacCollection, string KeyName)
        {
            if (HmacCollection?.Count() > 0 && HmacCollection.ContainsKey(KeyName))
            {
                return HmacCollection[KeyName];
            }
            return null;
        }
    }

    public class HMACSettings
    {
        public string ApiKey { get; set; } = string.Empty;
        public string SecretKey { get; set; } = string.Empty;
    }

我使用的是.Net Core,因此我示例化了HMac,当我需要密钥时,我只需调用

this.hmacSettings = hmac.Value?.GetServer(mlcKey);

如何在词典中添加另一层MLC。

idfiyjo8

idfiyjo81#

你总是可以嵌套字典。我冒昧地添加了一个类,虽然看起来“服务器”和“客户端”在你的情况下是一种常量。
新类如下所示:

public class ComputerGroup
{
    public IDictionary<string, HMACSettings> Servers { get; set; } = new Dictionary<string, HMACSettings>();
    public IDictionary<string, HMACSettings> Clients { get; set; } = new Dictionary<string, HMACSettings>();
}

您现有的HMAC类已修改,现在看起来如下所示:

public class HMAC
{
    public IDictionary<string, ComputerGroup> TopLevelGroup { get; set; } = new Dictionary<string, ComputerGroup>();

    public HMACSettings? GetServer(string topLevelKey, string keyName)
    {
        if (TopLevelGroup.ContainsKey(topLevelKey))
        {
            var topGroup = TopLevelGroup[topLevelKey];
            if (topGroup.Servers.ContainsKey(keyName))
            {
                return topGroup.Servers[keyName];
            }
        }
        return null;
    }
    
    public HMACSettings? GetClients(string topLevelKey, string keyName)
    {
        if (TopLevelGroup.ContainsKey(topLevelKey))
        {
            var topGroup = TopLevelGroup[topLevelKey];
            if (topGroup.Clients.ContainsKey(keyName))
            {
                return topGroup.Clients[keyName];
            }
        }
        return null;
    }
}

出于测试目的,我按如下方式填充设置:

hmac.TopLevelGroup.Add(
    "MLC", new ComputerGroup()
    {
        Servers = new Dictionary<string, HMACSettings>() {
            { "PE", new HMACSettings() { ApiKey = "pe-key", SecretKey = "pe-sec" } },
            { "DNB", new HMACSettings() { ApiKey = "dnb-key", SecretKey = "dnb-sec" } }
        },
        Clients = new Dictionary<string, HMACSettings>() {
            { "Billing", new HMACSettings() { ApiKey = "billing-key", SecretKey = "billing-sec" } },
            { "Finance", new HMACSettings() { ApiKey = "finance-key", SecretKey = "finance-sec" } }
        }
    }
);

使用hmac.GetServer("MLC", "PE");查询设置将返回// settings = HMACSettings { ApiKey = "pe-key", SecretKey = "secret-key" }

li9yvcax

li9yvcax2#

有一种非常简单的方法可以获取值hmac.Value?.GetServer(mlcKey);并将其转换为自定义类型:

string mlckey ="PE";
this.hmacSettings = configuration.GetSection("HMAC").GetSection("MLC").GetSection("Server").GetSection(mlckey).Get<HMACSettings>();

您需要从IConfiguration注入配置才能使用它。

相关问题