[英]grab UserSecrets from a NuGet package
我已經為我的公司編寫了一個 C# class 庫供內部使用,它使用 DotNet UserSecrets 允許每個開發人員設置自己的憑據,而無需擔心意外提交它們。 它在測試期間運行良好,但在將其安裝為 NuGet package 而不是項目依賴項之后,它似乎不再能夠從secrets.json
文件中讀取。 我想知道這是否是 C# 阻止的安全事件,或者我是否需要執行其他操作以在外部 package 中啟用該功能。
package 代碼如下所示:
using Microsoft.Extensions.Configuration;
using TechTalk.Specflow;
namespace Testing.Utilities
{
[Binding]
public class Context
{
private static IConfigurationRoot configuration { get; set; }
private static FeatureContext feature_context;
// SpecFlow attribute runs this before anything else executes
[BeforeFeature(Order = 1)]
private static void SetFeatureContext(FeatureContext context)
{
try
{
configuration = new ConfigurationBuilder()
.AddUserSecrets<Context>()
.Build();
}
catch { }
feature_context = context;
test_context = context.FeatureContainer.Resolve<TestContext>();
}
public static string GetSecretVariable(string name)
{
object v = null;
// if the user secrets were found
if (configuration != null)
{
v = configuration[name];
}
if (v == null)
{
Logger.Warning($"secret variable '{name}' not found");
return null;
}
return v.ToString();
}
}
}
在始終從 getter 方法獲取Null
的調用代碼中:
using Testing.Utilities; // via NuGet package
namespace Testing
{
public static void Main()
{
System.Console.WriteLine($"found {Context.GetSecretVariable("super_secret")}");
}
}
更新:當我將本地構建.nupkg
文件拖到我的 NuGet package 緩存中並替換從存儲庫中提取的文件時,它按預期工作。 我更新了版本號並推送了更改,所以我知道它們在同一版本上,並且它仍然只在我手動插入我的構建時有效。 現在我更迷茫了...
我將項目從 .NET Framework 4.6.1 移植到 .NET 6,它似乎修復了它。 有點劇烈的變化,但重構很容易,而且 461 無論如何都停產了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.