简体   繁体   English

未读取用户机密 - FileProvider 显示项目的调试路径

[英]User Secrets not being read - FileProvider is showing project's debug path

I have this weird issue where in one of the test projects in my solution the associated user secrets work well but in another new project they are not read at all, and when looking at the ConfigurationBuilder the FileProvider.Root is set to the debug folder.我有一个奇怪的问题,在我的解决方案中的一个测试项目中,关联的用户机密运行良好,但在另一个新项目中,它们根本没有被读取,并且在查看 ConfigurationBuilder 时,FileProvider.Root 被设置为调试文件夹。

Working:在职的: 在此处输入图像描述

Not Working:不工作: 在此处输入图像描述

To test I've put a secrets.json file in the debug folder and it indeed got read.为了测试,我在调试文件夹中放置了一个 secrets.json 文件,它确实被读取了。

The code for reading the config is pretty similar between the solutions and looks like this:读取配置的代码在解决方案之间非常相似,如下所示:

    public IConfigurationRoot GetConfig()
    {
        var configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddUserSecrets<ConfigClass1>()
            .AddUserSecrets<ConfigClass2>()
            ...
            .Build();
             
         return configuration;
     }

EDIT: The project does contain the UserSecretsId:编辑:该项目确实包含 UserSecretsId:

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UserSecretsId>bfe3...</UserSecretsId>
    <OutputType>Library</OutputType>
  </PropertyGroup>

I just came across this problem as well when trying to add UserSecrets in a.net 6 docker container.我在尝试在 a.net 6 docker 容器中添加 UserSecrets 时也遇到了这个问题。

The path you (and me) are getting stems from:你(和我)得到的路径源于:

https://source.dot.net/#Microsoft.Extensions.Configuration.FileExtensions/FileConfigurationExtensions.cs,41 https://source.dot.net/#Microsoft.Extensions.Configuration.FileExtensions/FileConfigurationExtensions.cs,41

The JsonConfigurationSource which UserSecrets use under the hood allows passing null as a file provider, and the JsonConfigurationSource will try to repair this by defaulting to AppContext.BaseDirectory?? string.Empty UserSecrets 在后台使用的 JsonConfigurationSource 允许将null作为文件提供程序传递,并且 JsonConfigurationSource 将尝试通过默认为AppContext.BaseDirectory?? string.Empty AppContext.BaseDirectory?? string.Empty . AppContext.BaseDirectory?? string.Empty .空。

Wrapping it back up, the provider initially is null due to this part:将其包装起来,由于这部分,提供者最初是 null:

https://source.dot.net/#Microsoft.Extensions.Configuration.UserSecrets/UserSecretsConfigurationExtensions.cs,172 https://source.dot.net/#Microsoft.Extensions.Configuration.UserSecrets/UserSecretsConfigurationExtensions.cs,172

which ultimately tries to determine whether the result of最终试图确定结果是否

https://source.dot.net/#Microsoft.Extensions.Configuration.UserSecrets/PathHelper.cs,26 https://source.dot.net/#Microsoft.Extensions.Configuration.UserSecrets/PathHelper.cs,26

    public static string GetSecretsPathFromSecretsId(string userSecretsId)
    {
        if (string.IsNullOrEmpty(userSecretsId))
        {
            throw new ArgumentException(SR.Common_StringNullOrEmpty, nameof(userSecretsId));
        }

        int badCharIndex = userSecretsId.IndexOfAny(Path.GetInvalidFileNameChars());
        if (badCharIndex != -1)
        {
            throw new InvalidOperationException(
                string.Format(
                    SR.Error_Invalid_Character_In_UserSecrets_Id,
                    userSecretsId[badCharIndex],
                    badCharIndex));
        }

        const string userSecretsFallbackDir = "DOTNET_USER_SECRETS_FALLBACK_DIR";

        // For backwards compat, this checks env vars first before using Env.GetFolderPath
        string? appData = Environment.GetEnvironmentVariable("APPDATA");
        string? root = appData                                                                   // On Windows it goes to %APPDATA%\Microsoft\UserSecrets\
                   ?? Environment.GetEnvironmentVariable("HOME")                             // On Mac/Linux it goes to ~/.microsoft/usersecrets/
                   ?? Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
                   ?? Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
                   ?? Environment.GetEnvironmentVariable(userSecretsFallbackDir);            // this fallback is an escape hatch if everything else fails

        if (string.IsNullOrEmpty(root))
        {
            throw new InvalidOperationException(SR.Format(SR.Error_Missing_UserSecretsLocation, userSecretsFallbackDir));
        }

        return !string.IsNullOrEmpty(appData)
            ? Path.Combine(root, "Microsoft", "UserSecrets", userSecretsId, SecretsFileName)
            : Path.Combine(root, ".microsoft", "usersecrets", userSecretsId, SecretsFileName);
    }

is valid.已验证。

As you can see, the GetSecretsPathFromSecretsId is trying quite a few options in order to determine the location of the secrets.json containing your secrets.如您所见,GetSecretsPathFromSecretsId 尝试了很多选项以确定包含您的机密的 secrets.json 的位置。 In my case it defaulted to the /home directory since there is no appdata on linux. I wasn't expecting this, and because I did not mount my user secrets into that non-default directory there just wasn't a directory at all, so the checks I lined out determined my fileprovider to be null and defaulted to AppContext.BaseDirectory.在我的例子中,它默认为 /home 目录,因为 linux 上没有应用数据。我没想到会这样,因为我没有将我的用户机密装载到那个非默认目录中,所以根本就没有目录,所以我列出的检查确定我的文件提供者是 null 并默认为 AppContext.BaseDirectory。

Hope this helps understanding the issue.希望这有助于理解问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM