jcr*_*ruz 5 c# .net-core asp.net-core blazor blazor-webassembly
我有一个 Blazor WebAssembly 应用程序,托管在公司网络的内部服务器上,例如:https://internal.domain.com/app。该应用程序可通过反向代理从外部访问:https://external.domain.com/app。
在 Blazor/C# 代码中,我们使用 .NET CoreNavigationManager设置一些重定向、路径等,这些在内部访问时可以完美运行。但在外部访问时我们遇到了一个问题,因为 的NavigationManagerURI 值是使用内部服务器地址返回的。
我们有一些代码将 HTML<base>元素的 href 值设置为应该拾取的外部地址NavigationManager。但是,导航管理器返回/期望的值仍然是内部地址,这会导致我们的路径和重定向出现问题。如文档中所述,NavigationManager.BaseUri 通常:
...对应于
href文档<base>元素上的属性。
我找不到有关上述声明或如何/是否可以初始化导航管理器的基本 URI 的其他信息。
有谁知道.NET Core 如何NavigationManager使用 HTML Base 元素?在这种情况下如何设置使用外部地址?
TIA
编辑:
下面是登录场景的示例代码:
index.html包含一些 JavaScript 来设置<base>元素和相应的外部 URL。检查页面标记时,使用外部 URL 按预期设置基本 href 值。App.razor对RedirectToLogin.razor组件的任何未经身份验证的请求。Navigation.Uri(类似于 的绝对值Navigation.BaseUri)。根据文档,这应该使用基本 href 值,但它不会选取index.html(外部 URL)中设置的值。wwwroot/index.html:
<head>
<script type="text/javascript">
var appBase = document.createElement('base');
appBase.href = "/app/";
//not actual if condition, but same logic
if (window.location.href.startsWith("https://external.domain.com")) {
appBase.href = "https://external.domain.com/app/";
}
document.head.appendChild(appBase);
</script>
</head>
Run Code Online (Sandbox Code Playgroud)
应用剃刀
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@if (!context.User.Identity.IsAuthenticated)
{
<RedirectToLogin />
}
</NotAuthorized>
</AuthorizeRouteView>
</Found>
</Router>
</CascadingAuthenticationState>
Run Code Online (Sandbox Code Playgroud)
重定向到登录.razor
@inject NavigationManager Navigation
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@code {
protected override void OnInitialized()
{
var uri = Navigation.Uri;
if (!uri.EndsWith("/"))
uri += "/";
Navigation.NavigateTo($"authentication/login?returnUrl={Uri.EscapeDataString(uri)}");
}
}
Run Code Online (Sandbox Code Playgroud)
“有谁知道 .NET Core NavigationManager 如何使用 HTML Base 元素?”
调用WebAssemblyHostBuilder JS 来获取 baseUri,它通过几跳获取值document.baseUri
| 归档时间: |
|
| 查看次数: |
1078 次 |
| 最近记录: |