安全存储Blazor Webassembly应用程序的应用程序机密

时间:2020-07-02 15:35:30

标签: blazor blazor-webassembly secret-manager

我正在寻找在blazor webassembly应用程序中安全存储应用程序秘密的方式。我们可以在下面的MSDN文档中找到有关服务器端应用程序的详细信息。

https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-3.1&tabs=windows

我们如何在完全在客户端浏览器中运行的Blazor WebAssembly应用程序中使用这些秘密?

我的基本情况是,需要将密码,产品密钥(许可密钥)信息保留在应用程序代码之外。例如,我们在Program.cs的静态main方法内部加载许可证。

https://i.stack.imgur.com/kCrV1.png

 public class Program
    {
        public static async Task Main(string[] args)
        {
            //want to access the product key here and need to avoid hardcoding
            SomeThirdPartyLibrary.RegisterLicense("product-key");
            var builder = WebAssemblyHostBuilder.CreateDefault(args);
            builder.RootComponents.Add<App>("app");

            builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

            await builder.Build().RunAsync();
        }

我已经搜索了blazor的文档,但无法找到任何详细信息。请帮助我在Blazor网站中找到解决此问题的推荐方法。

(对于服务器端,我们有多种选择,但是对于客户端,这是推荐的方式)

2 个答案:

答案 0 :(得分:1)

如果将其存储在客户端上,那是不安全的。

有一个实验性的MS nuget软件包,声称通过加密可以使存储安全-Microsoft.AspNetCore.ProtectedBrowserStorage

您可以在https://docs.microsoft.com/en-us/aspnet/core/blazor/state-management?view=aspnetcore-3.1

处阅读如何使用它。

答案 1 :(得分:0)

您可以使用MemoryConfigurationSource

使用内存配置

示例:

var appsettings = new Dictionary<string, string>()
{
   { "API:Key", "12345" }
};
var config = new MemoryConfigurationSource(InitialData = appsettings);
builder.Configuration.Add(memoryConfig);

然后无论您想使用它是什么,只需@inject配置(在剃须刀页面中)或您的类程序中,它们将类似于:

builder.Configuration.GetValue<string>("API:Key")