![](/img/trans.png)
[英]Why does this work when I debug from Visual Studio but doesn't after I deploy to IIS?
[英]Windows authentication doesn't work after deploy on iis
我对这段简单的代码有疑问:
public void OnAuthorization(AuthorizationFilterContext context)
{
ClaimsPrincipal user = _httpContextAccessor.HttpContext.User;
ClaimsIdentity identity = user.Identity as ClaimsIdentity;
string userName = identity.Name; //!!!
_logger.Trace("windows user `{0}` is trying to access the system", userName);
var admins = _configurationRoot.GetSection(ConfigDescription.Admins).Get<List<string>>();
if (!admins.Contains(userName))
{
_logger.Trace("Permission denied.");
context.Result = new RedirectResult("/error/unauthorized", false);
}
}
当我在Visual Studio
中通过IIS Express
启动我的asp net
应用程序时,一切正常。 在这种情况下我的日志:
2021-12-25 22:02:53.1783 TRACE windows user `Domain\username` is trying to access the system.
但在远程userName
上发布后,用户名始终为空。
2021-12-25 19:11:55.2524 TRACE windows user `` is trying to access the system.
2021-12-25 19:11:55.2524 TRACE Permission denied.
我正在尝试从 localhost 和通过域名打开网站,也将其添加到受信任的站点中,但没有任何帮助。
web.config
:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" forwardWindowsAuthToken="true" arguments=".\BlaBla.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
在 IIS 上启用匿名身份验证
因为如果没有,我什至无法打开这样的error/unauthorized
的页面:
要在 IIS 中启用 windows 身份验证,需要确保以下内容
1.在IIS中启用Windows认证
我们需要在“Windows features”中启用 Windows 身份验证(运行命令: optionalfeatures.Win + R → optionalfeatures )
2.在IIS web应用程序中启用Windows认证
然后我们需要为应用程序启用 windows 身份验证。 可以在 web.config 或 IIS 中完成
web.config
<system.webServer>
<security>
<authentication>
<windowsAuthentication enabled="true" />
<anonymousAuthentication enabled="false" />
</authentication>
</security>
</system.webServer>
IIS
启用 Windows 身份验证并禁用匿名身份验证。
更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.