31

After deploying ASP.NET Core app to azure and opening the site, I get the following error:

InvalidOperationException: Could not find 'UserSecretsIdAttribute' on assembly '******, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

The exception details also include that the error happens at Startup.cs on this line of code:

builder.AddUserSecrets();

Thank you

3 Answers 3

44

There was an update to the user secrets module just recently. Version 1.0.1 and up now requires you specify an assembly-level attribute for the id of the user secrets, or as a fallback, the way it was previously in project.json.

Here is the announcement on GitHub: https://github.com/aspnet/Announcements/issues/209

You can define the secrets id in the .csproj like this:

<PropertyGroup>
  <UserSecretsId>aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed</UserSecretsId>
</PropertyGroup>

This generates the following assembly-level attribute. Alternatively, instead of adding it in the .csproj file, you can of course add it yourself e.g. to Startup.cs:

[assembly: UserSecretsId("aspnet-TestApp-ce345b64-19cf-4972-b34f-d16f2e7976ed")]

Also, you should use:

builder.AddUserSecrets<Startup>();

It will search for that attribute in the assembly of the given type, in this case I used the Startup class.

Note: this will be deprecated in 2.0: (1.0.2 and 1.1.1 have marked it obsolete)

builder.AddUserSecrets();

I checked the source code for the user secrets configuration, and calling AddUserSecrets() without the type does this:

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);

It's trying to find the UserSecretsId attribute on your assembly, and failing that, checking if it could find it in project.json. Then (as commented) returns an error about the missing attribute as they wouldn't want to complain about project.json anymore as it is being deprecated.

6
  • Also, if you don't have the Microsoft.Extensions.Configuration.UserSecrets dependency in the project that has the UserSecretsId property, the attribute will not be created. Jul 10, 2017 at 13:39
  • If you don't have that dependency, I don't think you can use user secrets at all.
    – juunas
    Jul 10, 2017 at 13:40
  • I was depending on in transiently (via a "base" project I had sharing boilerplate between many projects in a solution). When I was using it transiently, the assembly attribute was missing, but when I added the dependency directly, the error went away. Jul 10, 2017 at 21:49
  • That's pretty interesting. I assume if you added the attribute manually it would have worked?
    – juunas
    Jul 11, 2017 at 5:53
  • Yes, if I manually add the attribute, it works fine. I guess the dependency adds something to the build process that generates the attribute from the project file? Jul 11, 2017 at 6:07
5

I want to add to this answer, for those in my situation.

I am writing a .NET Core console app, trying to use the secrets manager (not sure it's meant for console apps). The only way I was able to rid myself of the error was using the assembly level attribute on the assembly where I was using the secrets manager.

As I said, I am not sure if the secrets manager is meant for console apps. So maybe there is an issue with .xproj files vs. .csproj files.

3
  • Have you tried to add userSecretsId to project.json?
    – Alex78191
    Jan 8, 2017 at 7:57
  • @Alex78191 i already had it in project.json this error happened after i tried to migrate to multiple projects. I am using VS code Jan 20, 2017 at 11:19
  • This link goes on to great details in how to do this and it helped me get this figured out. twilio.com/blog/2018/05/…
    – FabianVal
    Aug 17, 2020 at 18:10
2

My .NET Core 3.1 Worker Service required additional setup (more than a Web project).

In Program.cs in the CreateHostBuilder method I needed this:

.ConfigureAppConfiguration((ctx, builder) =>
                {
                    // enable secrets in development
                    if (ctx.HostingEnvironment.IsDevelopment())
                    {
                        builder.AddUserSecrets<Worker>();
                    }
                })

But (unlike my Web project) I explicitly needed to add this nuget package:

install-package Microsoft.Extensions.Configuration.UserSecrets

After that I could access secrets.

2
  • 1
    Thank you @aage - that was exactly the problem I also ran into. I am using Host.CreateDefaultBuilder(args) and only needed to add the Microsoft.Extensions.Configuration.UserSecrets package, although it was already implictly available... confusing.
    – cbley
    Jun 11, 2021 at 14:42
  • Apparently, this was caused by an old version of Microsoft.Extensions.Hosting -- I had version 3.1.8 which was automatically included when generating the project with dotnet new. After upgrading to version 5.0.0 it is working fine without adding the Microsoft.Extensions.Configuration.UserSecrets package explicitly.
    – cbley
    Jun 11, 2021 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.