当在IIS或Asp.Net中启用并运行windows身份验证时,IIS似乎无法从调用context?.User?.Identity?.Name中识别用户。
想要的行为:在IIS和/或IIS中启用Windows身份验证和匿名身份验证,Asp.Net核心应该自动识别windows用户。
实际行为:当我在IIS或IIS Express中同时启用窗口和匿名身份验证时,用户名为空。当我禁用匿名身份验证或调用HttpContext.ChallengeAsync(IISDefaults.AuthenticationScheme)时,我会得到一个登录提示,这是我不想要的。
我的理解是,即使我想将它用于Active Directory,我也不需要active目录或域来验证windows用户。
环境:
依赖关系:
启动:
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 )
<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>发布于 2018-05-18 12:53:06
几件事:
[Authorize]属性。在允许匿名访问标题下,文档中有更多的细节。https://stackoverflow.com/questions/50400393
复制相似问题