8

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.

Yoda
  • 17,363
  • 67
  • 204
  • 344

5 Answers5

10

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.

Jun
  • 441
  • 4
  • 11
  • I used shared assembly that why I need to add this parameter it manually in the code(runtime) and at project level(compile time for VS). – Yoda Nov 16 '20 at 08:58
  • var attribute = assembly.GetCustomAttribute(); won't able to get the UserSecretsId in .csproj if GenerateAssemblyInfo is set to false. try debug this code – Jun Nov 17 '20 at 15:05
  • @Yoda I'm having the same issue (i.e., CreateDefaultBuilder is not adding secrets.json to config provider, ASPNETCORE_ENVIRONMENT is Development, and the UseSecretsId is in the .csproj file) and I also have SharedAssemblyInfo.cs. How did you add the UserSecretsId to the SharedAssemblyInfo.cs file? – Mike Jan 20 '21 at 05:10
  • @Mike I haven't you need usersecretsID in the csproj(for Visual Studio) and in the startup class as an annotation(for runtime). Then it will work. – Yoda Jan 20 '21 at 09:48
  • @Yoda Thanks for the response! I'm not following, what are you referring to when you say "in the startup class as an annotation"? Could you provide a code snippet? Thanks again. – Mike Jan 20 '21 at 13:38
  • 2
    @Mike. You can add it in startup class or SharedAssemblyInfo.cs `[assembly: UserSecretsId("XXXguidXXX")]` – Yoda Jan 20 '21 at 14:07
  • 1
    @Yoda Adding it to SharedAssemblyInfo.cs did the trick, thanks again! – Mike Jan 20 '21 at 14:34
  • @Mike. Upvote question if you may, thanks. – Yoda Jan 20 '21 at 15:42
  • 2
    @Yoda - I had the same issue and fixed in the same way. Took me hours to find this answer. Thanks so much for the solution! – Daniel Mackay May 05 '21 at 09:59
7

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.

Yifan Ai
  • 116
  • 1
  • 2
  • 10
sergeyxzc
  • 515
  • 6
  • 4
  • I had the same issue. At the beginning, the AssemblyInfo was generated automatically so everything worked fine. Then suddenly I need to set to false and things fell in parts. Took me hours to found out that. Who knows modifying assembly info can break secrets.json huh? – Dinh Tran Jan 28 '22 at 04:41
  • This solution also worked for me in .NET Framework 4.8 project – Karol Berezicki Feb 15 '23 at 21:16
4

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>();
            });
Darren
  • 335
  • 2
  • 11
  • Everything that you've provided is already done. However I have SharedAssemblyInfo.cs in the project Properties and if I put the id there it will be loaded during runtime however VS needs it also in csproj to be aware of secrets file. It's weird. – Yoda Sep 29 '20 at 14:34
1

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.

Andre
  • 81
  • 1
  • 3
  • I was going to do just that, but for some reason Visual Studio failed to include that option in the context menu. It was also hidden from the Project menu. At which point I starting searching the internet and wound up here. – Zarepheth Oct 18 '22 at 17:16
0

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 21 '23 at 12:45