从NuGet包中抓取UserSecrets



我为我的公司编写了一个内部使用的c#类库,它使用DotNet UserSecrets允许每个开发人员设置自己的凭据,而不必担心不小心提交它们。它在测试期间工作得很好,但在将其安装为NuGet包而不是项目依赖项之后,它似乎不再能够从secrets.json文件中读取。我想知道这是否是c#阻止的安全问题,或者我是否需要做其他事情来在外部包中启用该功能。

包代码如下所示:

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包缓存中并替换从repo中拉出的文件时,它就像预期的那样工作了。我更新了版本号并推动了更改,因此我知道它们在相同的版本上,并且它仍然只有在我手动插入构建时才能工作。现在我更困惑了……

我将项目从。net Framework 4.6.1移植到。net 6,它似乎解决了这个问题。有点剧烈的变化,但很容易重构,无论如何461是EOL。

最新更新