Asp.Net 核心 Windows 身份验证在 IIS 中不起作用
Asp.Net Core Windows Authentication Not Working in IIS
Asp.Net 当启用 windows 身份验证和在 IIS Express 或 IIS 中 运行 时,核心似乎无法识别调用 context?.User?.Identity?.Name
的用户。
期望的行为:在 IIS and/or 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 带 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
}));
});
launchSettings.json:
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:51682/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
web.config:
<?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 控制面板)-> 安全选项卡 -> 单击 'Trusted Sites' -> 单击 'Sites' 并将 URL 添加到您的站点。 IE 和 Chrome 都使用这些设置来决定是否自动发送您的凭据。
当您同时启用 匿名和Windows 身份验证时,匿名优先,除非您告诉它用户必须登录的地方in. 为此,请在控制器上或仅在单个操作上使用 [Authorize]
属性。在 Allow Anonymous Access.
标题下的文档中有更多详细信息
Asp.Net 当启用 windows 身份验证和在 IIS Express 或 IIS 中 运行 时,核心似乎无法识别调用 context?.User?.Identity?.Name
的用户。
期望的行为:在 IIS and/or 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 带 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
}));
});
launchSettings.json:
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:51682/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
web.config:
<?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 控制面板)-> 安全选项卡 -> 单击 'Trusted Sites' -> 单击 'Sites' 并将 URL 添加到您的站点。 IE 和 Chrome 都使用这些设置来决定是否自动发送您的凭据。
当您同时启用 匿名和Windows 身份验证时,匿名优先,除非您告诉它用户必须登录的地方in. 为此,请在控制器上或仅在单个操作上使用
标题下的文档中有更多详细信息[Authorize]
属性。在 Allow Anonymous Access.