MAUI: 获取用户密钥返回 null

huangapple go评论76阅读模式
英文:

MAUI: Getting user secrets returns null

问题

I did:

dotnet user-secrets init

然后

dotnet user-secrets set "con" "123"

在我的文件夹下:

/Users/juliustolksdorf/.microsoft/usersecrets/6b18f6d2-fda1-4673-b259-54176edbf6cc/secrets.json

我找到了一个名为 secrets.json 的 JSON 文件,其中包含了我的秘密。

我的 csprof 文件现在包括:

<UserSecretsId>6b18f6d2-fda1-4673-b259-54176edbf6cc</UserSecretsId>

我在代码中尝试的一切都是:

var con = Environment.GetEnvironmentVariable("con");

但是返回的是 null。
我漏掉了什么吗?似乎一切都应该正常工作...

英文:

I did:

dotnet user-secrets init

then

dotnet user-secrets set &quot;con&quot; &quot;123&quot;

In my folder under:

/Users/juliustolksdorf/.microsoft/usersecrets/6b18f6d2-fda1-4673-b259-54176edbf6cc/secrets.json

I found a json secrets.json now containing my secret.

My csprof file now includes:

&lt;UserSecretsId&gt;6b18f6d2-fda1-4673-b259-54176edbf6cc&lt;/UserSecretsId&gt;

And all I am trying in code is:

var con = Environment.GetEnvironmentVariable(&quot;con&quot;);

But null is returned.
Am I missing something? Seems like everything SHOULD be working...

答案1

得分: 1

抱歉,我明白你要求只返回翻译好的部分,这里是翻译结果:

"今天的.NET MAUI中没有内置支持,所以不幸的是它不起作用。

您可以在此问题中跟踪状态:https://github.com/dotnet/maui/issues/4408

如果您想今天使用这个功能,还提到了在项目中实现这个功能的方法。下面是关键代码,取自此处,完全归功于Maksym

public static class Keys
{
    public static string SyncfusionLicenseKey => Configuration?.Value<string>(nameof(SyncfusionLicenseKey)) ?? throw new NullReferenceException(nameof(SyncfusionLicenseKey));
    public static string MicrosoftAppCenterKey => Configuration?.Value<string>(nameof(MicrosoftAppCenterKey)) ?? throw new NullReferenceException(nameof(MicrosoftAppCenterKey));
    public static string CistApiKey => Configuration?.Value<string>(nameof(CistApiKey)) ?? throw new NullReferenceException(nameof(CistApiKey));

    private static JObject? Configuration { get; } = GetConfiguration("secrets.json");

    private static JObject? GetConfiguration(string filePath)
    {
        Assembly assembly = Assembly.GetExecutingAssembly();
        string? assemblyName = assembly.GetName().Name;
        using Stream? fileStream = assembly.GetManifestResourceStream($"{assemblyName}.{filePath}");
        if (fileStream == null)
        {
            return null;
        }

        using StreamReader sr = new(fileStream);
        string fileContent = sr.ReadToEnd();
        return JObject.Parse(fileContent);
    }
}

基本上,这只是将JSON文件作为清单文件加载并解析的操作。

您甚至可以通过使用文件系统助手,使这段代码更易于在.NET MAUI中使用。

英文:

There is no built-in support for this in .NET MAUI today, that's why it doesn't work unfortunately.

You can track the status in this issue: https://github.com/dotnet/maui/issues/4408

There is also a mention of a way of implementing this in your project if you want to use this today. The key code is pasted below, taken from here, full credit to Maksym:

    public static class Keys
    {
        public static string SyncfusionLicenseKey =&gt; Configuration?.Value&lt;string&gt;(nameof(SyncfusionLicenseKey)) ?? throw new NullReferenceException(nameof(SyncfusionLicenseKey));
        public static string MicrosoftAppCenterKey =&gt; Configuration?.Value&lt;string&gt;(nameof(MicrosoftAppCenterKey)) ?? throw new NullReferenceException(nameof(MicrosoftAppCenterKey));
        public static string CistApiKey =&gt; Configuration?.Value&lt;string&gt;(nameof(CistApiKey)) ?? throw new NullReferenceException(nameof(CistApiKey));

        private static JObject? Configuration { get; } = GetConfiguration(&quot;secrets.json&quot;);

        private static JObject? GetConfiguration(string filePath)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            string? assemblyName = assembly.GetName().Name;
            using Stream? fileStream = assembly.GetManifestResourceStream($&quot;{assemblyName}.{filePath}&quot;);
            if (fileStream == null)
            {
                return null;
            }

            using StreamReader sr = new(fileStream);
            string fileContent = sr.ReadToEnd();
            return JObject.Parse(fileContent);
        }
    }

Basically what this does is just load the json file as a manifest file and parse that.

You could maybe even make that code easier for .NET MAUI by using the file system helpers.

huangapple
  • 本文由 发表于 2023年5月25日 19:37:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定