Could not find 'UserSecretsIdAttribute' on assembly but it exists and correct package is added

5.5k Views Asked by At

I follow this tutorial: https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-3.1&tabs=windows#access-a-secret

To a .NET Core 3.1 project I've added Microsoft.Extensions.Configuration.UserSecrets package, I have clicked on the project to manage secrets, the file has been created in the AppData directory and the UserSecretsId has been added automatically in .csproj file. Because the Host.CreateDefaultBuilder didn't load secrets I decided to add them manually

 if (hostContext.HostingEnvironment.IsDevelopment())
        {
            builder.AddUserSecrets(Assembly.GetExecutingAssembly());
        }
    })
  .Build();

But then I get

System.InvalidOperationException
  HResult=0x80131509
  Message=Could not find 'UserSecretsIdAttribute' on assembly 'XXX'.
Check that the project for 'XXX' has set the 'UserSecretsId' build property.
If the 'UserSecretsId' property is already set then add a reference to the Microsoft.Extensions.Configuration.UserSecrets package.
  Source=Microsoft.Extensions.Configuration.UserSecrets

I've checked the suggestion in the expecion's description and both predicaments are fulfilled.

5

There are 5 best solutions below

1
On

You should have the UserSecretsId listed in your .csproj file. If you right click on the API layer and manage user secrets confirm that the GUID matches in the AppData directory with whats in the project.

<PropertyGroup>
<UserSecretsId>Your GUID</UserSecretsId>

Your Program.cs class should be using the CreateDefaultBuilder, you should see that there is a section to include Secrets when the environment is Development. Check your ASP "ASPNETCORE_ENVIRONMENT": "Development" is set to development. Lastly I'm assuming your executing assembly has the PropertyGroup for the user secrets mentioned above.

 public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
3
On

Faced the same runtime error. I have disabled the automatic creation of the AssemblyInfo for assembly in the csproj, because my system CI automatically updates this file with the version of the build

My fix is to add

[assembly: UserSecretsId ("<Project guid>")] 

in AssemblyInfo.

1
On

Just use the context menu on your project and choose "Manage User Secrets..." and save the empty user secrets configuration file. Visual Studio should create the necessary project file entries automatically.

9
On

Same issue here. after check the source code in GitHub https://github.com/aspnet/Configuration/blob/f64994e0655659faefccead7ccb5c1edbfd4d4ba/src/Config.UserSecrets/UserSecretsConfigurationExtensions.cs#L94

Check if you have <GenerateAssemblyInfo>true</GenerateAssemblyInfo> in your .csproj file as well.

1
On

System.InvalidOperationException: 'Could not find 'UserSecretsIdAttribute' on assembly ''. Check that the project for '' has set the 'UserSecretsId' build property. If the 'UserSecretsId' property is already set then add a reference to the Microsoft.Extensions.Configuration.UserSecrets package.'

Adding this package solved the issue. Microsoft.Extensions.Configuration.UserSecrets