我需要一些帮助来使用字典从 appSettings 中检索它,而无需对值进行硬编码。
"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