【问题标题】:Host Blazor WebAssembly with Authentication project on windows IIS在 Windows IIS 上使用身份验证项目托管 Blazor WebAssembly
【发布时间】:2021-10-18 07:37:28
【问题描述】:

我刚刚使用身份验证项目创建了 Blazor WebAssembly,并没有更改代码上的任何内容,然后尝试在 Windows IIS 上托管该项目,当网站运行时它总是给我这个错误:

("crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Could not load settings from '_configuration/MyWebstieAut.Client'
      Error: Could not load settings from '_configuration/MyWebstieAut.Client'
         at a.createUserManager (http://mywebsiteaut.com/_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js:1:5887)
         at Generator.prototype.next (native code)
Microsoft.JSInterop.JSException: Could not load settings from '_configuration/MyWebstieAut.Client'
Error: Could not load settings from '_configuration/MyWebstieAut.Client'
   at a.createUserManager (http://mywebsiteaut.com/_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js:1:5887)
   at Generator.prototype.next (native code)
  at System.Threading.Tasks.ValueTask`1[TResult].get_Result () <0x21bcc70 + 0x0002c> in <filename unknown>:0 
  at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsyn")

你能帮帮我吗?

谢谢

program.cs:

 var builder = WebAssemblyHostBuilder.CreateDefault(args);
            builder.RootComponents.Add<App>("#app");
            builder.Services.AddHttpClient("{ProjectName}.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
                .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

            // Supply HttpClient instances that include access tokens when making requests to the server project
            builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("{ProjectName}.ServerAPI"));
            builder.Services.AddApiAuthorization();

            await builder.Build().RunAsync();

appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-{ProjectName}.Server-A5A9DCDA-10E3-45AE-A34F-DEF0D6A04784;Trusted_Connection=True;MultipleActiveResultSets=true",
    "{ProjectName}ServerContextConnection": "Server=(localdb)\\mssqllocaldb;Database={ProjectName}.Server;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "IdentityServer": {
    "Clients": {
      "{ProjectName}.Client": {
        "Profile": "IdentityServerSPA"
      },
"Key": {
  "Type": "Store",
  "StoreName": "WebHosting",
  "StoreLocation": "CurrentUser",
  "Name": "CN=MyApplication"
}
    }
  },
  "AllowedHosts": "*"
}

【问题讨论】:

    标签: authentication blazor blazor-webassembly


    【解决方案1】:

    检查您是否在 wwwroot 文件夹中发布了 appsettings.json。 在 WASM 项目中,客户端配置是从该位置的该文件中读取的。 而且您可能在另一个位置(不是 wwwroot)为您的服务器项目提供了另一个 appsettings.json。

    如果您选择了 Microsoft Identity 身份验证类型,则需要在 Blazor 客户端项目的 wwwroot 文件夹中添加一个 appsettings.json,如下所示:

    {
      /*
      The following identity settings need to be configured
      before the project can be successfully executed.
      For more info see https://aka.ms/dotnet-template-ms-identity-platform 
      */
      "AzureAd": {
        "Authority": "https://login.microsoftonline.com/22222222-2222-2222-2222-222222222222",
        "ClientId": "33333333-3333-3333-33333333333333333",
        "ValidateAuthority": true
      }
    }
    

    您还需要创建一个“密钥”以允许身份服务器按预期工作。
    信息在这里:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-5.0#example-deploy-to-a-non-azure-web-hosting-provider

    出于测试目的,您可以修改已部署的 appsettings.json 如下(参见 Key):

    {
      "ConnectionStrings": {
        "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-TestWasmAuthIndividual.Server-D7E2935A-AF1F-4B7C-9870-1288478498E1;Trusted_Connection=True;MultipleActiveResultSets=true"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "IdentityServer": {
        "Clients": {
          "TestWasmAuthIndividual.Client": {
            "Profile": "IdentityServerSPA"
          }
        },
        "Key": {
          "Type": "Development"
        }
      },
    "AllowedHosts": "*"
    }
    

    使用此配置,我已将我的 Server 项目(因为我选择 ASP.NET Core Hosted)部署到 Default Web Site 中的文件夹 在 IIS 中。
    如果您在子文件夹中发布,请记住在 index.html 中更改您的 &lt;base href...

    【讨论】:

    • 我将 appsetings.json 文件移动到 wwwroot 并确保发布文件夹中有一个 appsetings.json 文件,但没有工作
    • 能否分享您的 Program.cs(客户端)和您的 appsettings.json(客户端I)?
    • 我在问题中添加了它们
    • 您添加到答案中的 appsettings.json 用于 Blazor Server 项目。 Blazor 客户端 需要另一个。我在答案中添加了一些信息。
    • 我正在使用个人帐户类型并通过 IIS 托管
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2020-11-02
    • 1970-01-01
    • 2020-09-16
    • 2019-11-30
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多