英文:
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 "con" "123"
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:
<UserSecretsId>6b18f6d2-fda1-4673-b259-54176edbf6cc</UserSecretsId>
And all I am trying in code is:
var con = Environment.GetEnvironmentVariable("con");
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 => 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);
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论