c# - 使用 Windows 身份验证对 ASP.NET Core 基于角色的授权进行故障排除
问题描述
对于托管在 IIS 上的自包含服务器端 Blazor 应用程序,我们已将该应用程序配置为使用 Windows 身份验证和从 Active Directory 组派生的基于角色的授权。
在Startup.cs
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddAuthentication(IISDefaults.AuthenticationScheme);
//....
}
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
在应用程序中,我们使用<AuthorizeView />
组件和AuthenticationState
来控制访问。
我们有一个暂存环境和一个生产环境。据我所知,这两个环境的配置是相同的,除了在登台服务器上ASPNETCORE_ENVIRONMENT
设置为。Staging
暂存环境按预期工作。创建了一些诊断输出以尝试了解发生了什么:
@page "/diag"
<h3>Diag</h3>
<AuthorizeView>
<div>
User.Identity.IsAuthenticated: @context.User.Identity.IsAuthenticated
</div>
<div>
User.Identity.AuthenticationType: @context.User.Identity.AuthenticationType
</div>
<div>
AuthenticationStateContext.User.Identity.Name: @context.User.Identity.Name
</div>
<div>
User.IsInRole(Administrator): @context.User.IsInRole(Roles.Administrator)
</div>
</AuthorizeView>
Roles.Administrator
是与管理员的 AD 组名称匹配的常量字符串。
以管理员组中的用户身份登录时,登台服务器会产生预期的结果:
User.Identity.IsAuthenticated: True
User.Identity.AuthenticationType: Negotiate
AuthenticationStateContext.User.Identity.Name: DOMAIN\my.name
User.IsInRole(Administrator): True
但是生产服务器进行身份验证,但它不识别角色:
User.Identity.IsAuthenticated: True
User.Identity.AuthenticationType: Negotiate
AuthenticationStateContext.User.Identity.Name: DOMAIN\my.name
User.IsInRole(Administrator): False
据我所知,IIS 应用程序配置为 Windows 身份验证。匿名身份验证已禁用,Windows 身份验证已启用。Windows 身份验证似乎正在工作 - 但授权不是。
我很感激任何反馈。
编辑
在最初的问题之后继续进行故障排除。为每个站点打开浏览器开发者控制台并比较活动。注意到指向登台服务器的客户端只有一个使用 websocket url 的 blazor 请求。指向生产服务器的客户端使用 http 生成了许多 blazor 请求——我认为这意味着它已降级为长轮询。为什么不使用 websockets?经过进一步调查,我发现我的生产网络服务器没有安装 websocket 模块!安装后,应用程序按预期运行。
我只能得出结论,服务器端 blazor 的授权在长轮询时可能不会像预期的那样运行,但我不太确定要尝试回答我自己的问题。如果社区认为这是最好的,我会很乐意关闭它。也许有更好理解的人可以提供更完整的答案。
解决方案
希望我可以将您的编辑标记为答案。这对我来说也绝对是解决办法。在 Server 2016 IIS 上,我必须使用此处的说明安装 Websocket 模块:
https://docs.microsoft.com/en-us/iis/configuration/system.webserver/websocket
推荐阅读
- c# - 将 Xamarin 图像保存到文件
- swift - Swift Alamofire 构建错误标识符名称违规
- mysql - MYSQL:如何选择购买了一种产品但没有购买另一种产品的客户
- c# - 一个系统上的 C# 中的 Matlab MWMCR::EvaluateFunction 错误
- xamarin - 如何通过标记化 API 请求使 android 下载管理器下载
- arrays - 将具有不匹配属性的对象数组转换为 csv
- django - 在您的软件中实现国际化的正确方法是什么?
- elasticsearch - 为什么 Elasticsearch 使用 PUT 而不是 POST 来创建索引?
- linux - Percona Server MongoDB on Linux - 如何解决与 mongodb-org 的安装冲突?
- c++ - 高效的信号处理