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

3

I follow this tutorial: https://docs.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.

asp.net-core
secretsmanager
asked on Stack Overflow Sep 29, 2020 by Yoda

3 Answers

2

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.

answered on Stack Overflow Nov 16, 2020 by Jun
1

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>();
            });
answered on Stack Overflow Sep 29, 2020 by Darren
1

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

My fix is to add

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

in AssemblyInfo.

answered on Stack Overflow Apr 12, 2021 by sergeyxzc

User contributions licensed under CC BY-SA 3.0