blazor-server-side - 如何在 Blazor 服务器端应用程序中启用/修复 Windows 身份验证?
问题描述
当我们最初创建服务器端 Blazor 应用程序(ASP.NET Core Web 应用程序)时,我们没有启用身份验证。我们现在想启用 Windows 身份验证。
我创建了一个带有 Windows 身份验证的测试 Web 应用程序,并尝试将缺失的位添加到我们现有的 Web 应用程序中。以下是我所做的更改:
- 修改 app.razor 以包含 cascadingauthenticationstate
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<CascadingAuthenticationState>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</CascadingAuthenticationState>
</NotFound>
</Router>
- 在 _imports.razor 中添加了缺失的导入
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
- 尝试使用剃须刀组件中的以下代码块获取当前 Windows 身份:
@code {
[CascadingParameter]
private Task<AuthenticationState> authenticationStateTask { get; set; }
private async Task LogUsername()
{
var authState = await authenticationStateTask;
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
Console.WriteLine($"{user.Identity.Name} is authenticated.");
}
else
{
Console.WriteLine("The user is NOT authenticated.");
}
}
}
当我在 Visual Studio 2019 预览版中本地运行 Web 应用程序时,我总是以一个空的身份名称结束。而且,IsAuthenticated 总是错误的。但是,如果我在本地运行测试 Web 应用程序,它会获得正确的身份名称。
有谁知道我在现有的网络应用程序中缺少什么?
谢谢!
解决方案
显然,我错过了在“项目属性”>“调试”选项卡下选中 Windows 身份验证框。
推荐阅读
- python - Django相关模型未在管理员中更新相关对象
- python - Python beautifulsoup解析html
- ios - 是否可以在默认初始化代码运行后立即运行初始化代码?
- google-cloud-platform - GCP:从 Cloud Function 启动 Compute Engine 实例时无法设置元数据
- reason - 在 reasonML 中结合两个 switch case
- c# - 如何同时打印多个随机列表
- java - 排序算法中的程序中断块
- ios - 基于启动故事板加载屏幕偏离中心?
- javascript - 调用js函数时会调用cefsharp浏览器的哪个事件?
- node.js - nodejs api什么都不返回