首页 > 解决方案 > Blazor - app.UseIdentityServer(); 使用 .pfx 密钥文件 - 解析数字时遇到意外字符

问题描述

我创建了一个新的 Blazor WebAssembly 应用程序,其中包含个人用户帐户、应用程序内存储用户帐户和托管在 .NET 5 中的 ASP.NET Core。将我的应用程序部署到 Azure 应用程序服务时,出现以下错误:

对象引用未设置为对象的实例。在 Microsoft.Extensions.DependencyInjection.IdentityServerBuilderConfigurationExtensions

阅读这些链接,我必须为 IdentityServer 提供我自己的生产证书:

Blazor Web Assembly App .Net Core Hosted:发布运行时错误

https://stackoverflow.com/a/56904000/3850405

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-5.0#example-deploy-to-azure-app-service

然后我创建了一个.pfx这样的文件,我已经验证它可以工作并且我的密码是正确的。

https://stackoverflow.com/a/48790088/3850405

然后我将.pfx文件放在我的Server项目根文件夹中并标记Copy to Output DirectoryCopy Always.

然后我更新appsettings.json为如下所示:

  "IdentityServer": {
    "Clients": {
      "BlazorTest.Client": {
        "Profile": "IdentityServerSPA"
      }
    },
    "Key": {
      "Type": "File",
      "FilePath": "localhost.pfx",
      "Password": "MySercurePassword123?"
    }
  },

现在该项目在本地或 Azure 上都不起作用。它失败并app.UseIdentityServer();出现Startup.cs以下错误:

Newtonsoft.Json.JsonReaderException:'解析数字时遇到意外字符:�. 路径'',第 1 行,位置 1。

根据 Microsoft 文档,我的证书应该是有效的:

用于签署令牌的生产证书。

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-5.0#deploy-to-production

如果我像这样加载密钥,它可以工作:

"Key": {
  "Type": "Store",
  "StoreName": "My",
  "StoreLocation": "CurrentUser",
  "Name": "CN=blazortest"
}

标签: c#azureblazoridentityserver4blazor-webassembly

解决方案


我可以通过删除appsettings.Development.json中的 IdentityServer 条目来解决这个问题。

从 appsettings 中删除以下行。开发.json:

"IdentityServer": {
     "Key": {
       "Type": "Development"
     }
   } 

推荐阅读