gpt4 book ai didi

c# - 在 ASP.NET 中使用 Windows 身份验证

转载 作者:可可西里 更新时间:2023-11-01 07:42:49 25 4
gpt4 key购买 nike

我正在尝试在我的 ASP.NET 应用程序中使用 Windows 身份验证。每当我尝试查看该应用程序时,它都会将我发送到登录页面。如何在无需通过浏览器手动登录的情况下使其工作?

web.config

  <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>

更新 IIS Express 后出错

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>

This is the article

关于c# - 在 ASP.NET 中使用 Windows 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36946304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com