【问题标题】:Blazor WebAssembly Hosted on IIS - only API is working托管在 IIS 上的 Blazor WebAssembly - 只有 API 有效
【发布时间】:2022-05-08 05:22:42
【问题描述】:

我有一个项目,我使用 Blazor WebAssembly 托管模型开发了一个解决方案。我有一个服务器项目、一个客户端项目和一个共享项目(但我将其重命名为域)。

当我发布时,我发布到一个文件夹,因为我没有其他方法可以连接到我尝试部署到的服务器,该服务器是 Windows Server 2019 操作系统。

然后我将发布的文件夹添加到 IIS,但我只能让 API 端点工作。当我导航到应用程序的根目录时,即 https://localhost:5001 我只是收到 404 错误。

我已经安装了 .NET 的依赖项、URL 重写、添加了自签名证书等。

我不明白为什么这不起作用。当我在 Visual Studio 中启动时,它在使用 Release 和 Debug 标志时都可以正常工作。

谁能指导我做错了什么?

导航到 https://localhost:5001 时记录(不工作)

[10:11:24 信息] Microsoft.Hosting.Lifetime 应用程序已启动。 按 Ctrl+C 关闭。

[10:11:24 信息] Microsoft.Hosting.Lifetime 托管环境: 生产

[10:11:24 信息] Microsoft.Hosting.Lifetime 内容根路径: C:\Temp\Project\发布

[10:11:24 信息] Microsoft.AspNetCore.Hosting.Diagnostics 请求开始 HTTP/2 GET https://localhost:5001/ - -

[10:11:25 信息] Microsoft.AspNetCore.Routing.EndpointMiddleware 执行端点'Fallback {*path:nonfile}'

[10:11:25 信息] Microsoft.AspNetCore.Routing.EndpointMiddleware 执行端点'回退 {*path:nonfile}'

[10:11:25 信息] Microsoft.AspNetCore.Hosting.Diagnostics 请求完成 HTTP/2 GET https://localhost:5001/ - - - 404 - - 94.7963ms

导航到 https://localhost:5001/api/dataflows(有效)时的日志

[10:12:46 信息] Microsoft.AspNetCore.Hosting.Diagnostics 请求启动 HTTP/2 GET https://localhost:5001/api/dataflows - -

[10:12:47 信息] Microsoft.AspNetCore.Routing.EndpointMiddleware 执行的端点 'Project.Web.Server.Controllers.DataflowsController.GetDataflows (Project.Web.Server)'

[10:12:47 信息] Microsoft.AspNetCore.Hosting.Diagnostics 请求完成 HTTP/2 GET https://localhost:5001/api/dataflows - - - 200 2 应用程序/json;+charset=utf-8 619.7714ms

Project.Web.Server/Statup.cs:ConfigureServices

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseMigrationsEndPoint();
        app.UseWebAssemblyDebugging();
    }
    else
    {
        app.UseExceptionHandler("/Error");
    }

    app.UseBlazorFrameworkFiles();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseIdentityServer();
    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        endpoints.MapControllers();
        endpoints.MapFallbackToFile("{*path:nonfile}", "index.html"); ;
    });
}

Project.Web.Server/appsettings.json

{
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "Identity": "Data Source=|DataDirectory|\\IdentityDB.db",
    "Api": "Data Source=|DataDirectory|\\ApiDB.db",
    "Dataflows": "Data Source=|DataDirectory|\\DataflowsDB.db"
  },
  "IdentityServer": {
    "Clients": {
      "Project.Web.Client": {
        "Profile": "IdentityServerSPA"
      }
    },
    "Key": {
      "Type": "Store",
      "StoreName": "My",
      "StoreLocation": "CurrentUser",
      "Name": "CN=localhost"
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      "Microsoft.EntityFrameworkCore.Database.Command": "Warning"
    }
  },
  "Serilog": {
    "Using": [ "Serilog.Sinks.File" ],
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "C:\\Temp\\Project\\log.txt",
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithExceptionDetails" ]
  }
}

Project.Web.Client/Program.cs:ConfigureServices

public static Task Main(string[] args)
{
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.RootComponents.Add<App>("#app");

    builder.Services.AddHttpClient("Project.Web.Client.API", 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("Project.Web.Client.API"));

    // Add Open ID Connect client settings
    builder.Services.AddOidcAuthentication(options =>
    {
        builder.Configuration.Bind("oidc", options.ProviderOptions);
    }).AddAccountClaimsPrincipalFactory<RolesClaimsPrincipalFactory>();

    builder.Services.AddOptions();
    builder.Services.AddAuthorizationCore();
    builder.Services.AddApiAuthorization();
    return builder.Build().RunAsync();
}

Project.Web.Client/Shared/RedirectToLogin.razor

@inject NavigationManager Navigation
@code {
    protected override void OnInitialized()
    {
        Navigation.NavigateTo($"authentication/login?returnUrl={Uri.EscapeDataString(Navigation.Uri)}");
    }
}

Project.Web.Client/wwwroot/index.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>Project.Web</title>
    <base href="/" />
    <link href="/company/fonts/css/styles-technical.css" rel="stylesheet" />
    <link href="/company/icons/css/styles.css" rel="stylesheet" />

</head>

<body>
    <div id="app">Loading...</div>

    <script src="_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"></script>
    <script src="_framework/blazor.webassembly.js"></script>
    <script type="module">
        import { defineCustomElements } from '/company/web-ui/loader/index.js';
        defineCustomElements(window);
    </script>
    <script src="/scripts/utils.js"></script>
</body>

</html>

Project.Web.Client/wwwroot/appsettings.json

{
  "oidc": {
    "Authority": "https://localhost:5001/",
    "ClientId": "Project.Web.Client",
    "ResponseType": "code",
    "DefaultScopes": [
      "openid",
      "profile"
    ],
    "PostLogoutRedirectUri": "/",
    "RedirectUri": "https://localhost:5001/authentication/login-callback"
  }
}

【问题讨论】:

  • 如果您使用Intelligencia UrlRewriter,您可能需要在web.config 文件中添加一些配置,如post 所示。
  • 对不起,我不用那个包。

标签: c# iis blazor openid-connect webassembly


【解决方案1】:
猜你喜欢
  • 2021-06-25
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2020-05-25
  • 2020-12-13
相关资源
最近更新 更多