最近对用户机密模块进行了更新。 1.0.1 及更高版本现在要求您为用户机密的 id 指定程序集级属性,或者作为后备,它以前在 project.json 中的方式。
这里是 GitHub 上的公告:https://github.com/aspnet/Announcements/issues/209
您可以像这样在 .csproj 中定义秘密 ID:
<PropertyGroup>
<UserSecretsId>aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed</UserSecretsId>
</PropertyGroup>
这会生成以下程序集级属性。或者,您当然可以自己添加,而不是将其添加到 .csproj 文件中,例如到 Startup.cs:
[assembly: UserSecretsId("aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed")]
另外,你应该使用:
builder.AddUserSecrets<Startup>();
它将在给定类型的程序集中搜索该属性,在这种情况下,我使用了 Startup 类。
注意:这将在 2.0 中被弃用:(1.0.2 和 1.1.1 已将其标记为过时)
builder.AddUserSecrets();
我检查了source code 的用户机密配置,并在没有类型的情况下调用AddUserSecrets() 这样做:
var attribute = entryAssembly.GetCustomAttribute<UserSecretsIdAttribute>();
if (attribute != null)
{
return AddUserSecrets(configuration, attribute.UserSecretsId);
}
// try fallback to project.json for legacy support
try
{
var fileProvider = configuration.GetFileProvider();
return AddSecretsFile(configuration, PathHelper.GetSecretsPath(fileProvider));
}
catch
{ }
// Show the error about missing UserSecretIdAttribute instead an error about missing
// project.json as PJ is going away.
throw MissingAttributeException(entryAssembly);
它试图在你的程序集中找到 UserSecretsId 属性,但失败了,检查它是否可以在 project.json 中找到它。然后(如评论)返回有关缺少属性的错误,因为他们不想再抱怨 project.json 已被弃用。