iis - Asp.Net Core Windows 身份验证在 IIS 中不起作用
问题描述
context?.User?.Identity?.Name
当启用 Windows 身份验证并在 IIS Express 或 IIS 中运行时,Asp.Net Core 似乎无法从调用中识别用户。
期望行为:在 IIS 和/或 IIS Express 中同时启用 Windows 身份验证和匿名身份验证,Asp.Net Core 应自动识别 windows 用户。
实际行为:当我在 IIS 或 IIS Express 中同时启用 Windows 和匿名身份验证时,用户名为空。当我禁用匿名身份验证或调用HttpContext.ChallengeAsync(IISDefaults.AuthenticationScheme)
时,我会收到一个我不想要的登录提示。
我的理解是,即使我想将它用于 Active Directory,我也不需要 Active Directory 或域来验证 Windows 用户。
环境:
- Windows 8.1(不在域上)
- IIS 8.5 / Visual Studio 2017 w/ IIS Express
- 已安装 Windows 身份验证安全功能
- Windows 身份验证和(使用 NTLM 提供程序)和启用匿名身份验证
- 以本地帐户用户身份登录
依赖项:
- Microsoft.AspNetCore.All 2.0.8
启动:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<IISOptions>(iis =>
{
iis.AuthenticationDisplayName = "Windows";
iis.AutomaticAuthentication = true;
});
services.AddAuthentication(IISDefaults.AuthenticationScheme);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.Run(async (context) =>
{
await context.Response.WriteAsync(JsonConvert.SerializeObject(new
{
UserName = context?.User?.Identity?.Name
}));
});
启动设置.json:
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:51682/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
网络配置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<remove name="aspNetCore" />
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore forwardWindowsAuthToken="true" processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</configuration>
applicationhost.config:(IIS Express)
<authentication>
<anonymousAuthentication enabled="true" userName="" />
<basicAuthentication enabled="false" />
<clientCertificateMappingAuthentication enabled="false" />
<digestAuthentication enabled="false" />
<iisClientCertificateMappingAuthentication enabled="false"></iisClientCertificateMappingAuthentication>
<windowsAuthentication enabled="true">
<providers>
<add value="NTLM" />
</providers>
</windowsAuthentication>
</authentication>
解决方案
几件事:
当您禁用匿名身份验证时,您会收到一个弹出窗口,因为浏览器可能不信任该站点。您需要打开 Internet 选项(从 Windows 控制面板)-> 安全选项卡 -> 单击“受信任的站点”-> 单击“站点”,然后将 URL 添加到您的站点。IE 和 Chrome 都使用这些设置来决定是否自动发送您的凭据。
当您同时启用匿名和 Windows 身份验证时,匿名优先,除非在您告诉它用户必须登录的地方。为此,请
[Authorize]
在控制器上或仅在单个操作上使用该属性。文档中有更多详细信息,位于Allow Anonymous Access标题下。
推荐阅读
- javascript - 将自定义 javascript 添加到 wix
- html - 如何定位两个具有重叠角的 div,例如链中的链接?
- javascript - 在 onClick 处理程序中设置 useState 的问题
- android-recyclerview - 如何在 FrameLayout 上实现 RecyclerView?
- python - 以可以在 Python 中排序的格式存储输出
- angular - 如何创建一些计时器并显示它们的结果?
- python - TensorFlow 2.0 中的最小化没有按预期进行
- javascript - 在节点 express 中出现错误 ESOCKETTIMEDOUT
- xcode - Xcode 在结帐时出错:无法创建分支,因为一个名为“master”的分支已经存在
- linux - 将键:值转换为 CSV 文件