[英]Using Windows Authentication in ASP.NET
我正在尝试在我的ASP.NET应用程序中使用Windows身份验证。 每当我尝试查看应用程序时,它都会将我发送到登录页面。 如何在不通过浏览器手动登录的情况下使其工作?
<system.web>
<authentication mode="Windows"></authentication>
<anonymousIdentification enabled="false"/>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
<customErrors mode="Off"></customErrors>
<identity impersonate="true"></identity>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime />
</system.web>
Most likely causes:
No authentication protocol (including anonymous) is selected in IIS.
Only integrated authentication is enabled, and a client browser was used that does not support integrated authentication.
Integrated authentication is enabled and the request was sent through a proxy that changed the authentication headers before they reach the Web server.
The Web server is not configured for anonymous access and a required authorization header was not received.
The "configuration/system.webServer/authorization" configuration section may be explicitly denying the user access.
<authentication>
<anonymousAuthentication enabled="false" />
<basicAuthentication enabled="false" />
<clientCertificateMappingAuthentication enabled="false" />
<digestAuthentication enabled="false" />
<iisClientCertificateMappingAuthentication enabled="false">
</iisClientCertificateMappingAuthentication>
<windowsAuthentication enabled="true">
<providers>
<add value="Negotiate" />
<add value="NTLM" />
</providers>
</windowsAuthentication>
</authentication>
使用IISExpress进行Windows身份验证
更新您的web.config
确保您的web.config文件都启用了Windows身份验证,并且还拒绝匿名身份验证。 如果应用程序通过匿名身份验证, HttpContext.Current.User.Identity.Name
将为空。 您的配置应如下所示:
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
错误401.2未经授权有时,您可能会收到错误401.2 Unauthorized: Logon failed due to server configuration error
。 如果这样做,请验证您是否有权根据您提供的凭据查看此目录或页面。 还要确保在Web服务器上启用了身份验证方法。
更新applicationhost.config
您还可能会发现必须更新IISExpress applicationhost.config文件(不要担心 - 我也不知道)。 这基本上是IIS配置工具的文件版本,您可以在其中配置Web服务器本身。 查找applicationhost.config
文件可能很棘手。 它可能在:
%userprofile%\\documents\\iisexpress\\config\\applicationhost.config
要么
%userprofile%\\my documents\\iisexpress\\config\\applicationhost.config
找到后,更新以下行(特别注意enabled=true
):
<windowsAuthentication enabled="true">
<providers>
<add value="Negotiate" />
<add value="NTLM" />
</providers>
</windowsAuthentication>
我们对几乎所有的Intranet应用程序(包括SharePoint)都使用Windows身份验证。 如果员工的浏览器未自动将其Windows凭据自动发送到站点,则员工必须登录。
在IE上,这是浏览器配置的问题。 我认为还有一些方法可以配置Chrome和Firefox自动发送Windows登录。 我认为Chrome会像IE一样关注Window的互联网设置(在客户端上)。 尝试将用户身份验证选项设置为“使用当前用户名和密码自动登录”。
请参阅下面的屏幕截图,了解其位置。
另请注意,这涉及用户的浏览器将Windows令牌发送到应用程序。 应用程序必须理解并信任此令牌的来源,这可以在用户和应用程序所在的“域”的支持下工作。我认为它可以在单个机器上工作(在调试时),但是如果您希望这在网络上的多台计算机上运行,则需要考虑创建域。 创建域的典型方法是Active Directory。
让我知道。
在VS 2017中调试我的Web应用程序时,我发现需要更新[解决方案路径] \\。vs \\ config \\ applicationhost.config。 我将身份验证部分替换为:
<authentication>
<anonymousAuthentication enabled="false" userName="" />
<basicAuthentication enabled="false" />
<clientCertificateMappingAuthentication enabled="false" />
<digestAuthentication enabled="false" />
<iisClientCertificateMappingAuthentication enabled="false">
</iisClientCertificateMappingAuthentication>
<windowsAuthentication enabled="true">
<providers>
<add value="Negotiate" />
<add value="NTLM" />
</providers>
</windowsAuthentication>
</authentication>
通过删除协商提供程序,我能够让它工作。
<windowsAuthentication enabled="true">
<providers>
<add value="NTLM" />
</providers>
</windowsAuthentication>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.