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

Ant*_*yrd 15 c# asp.net iis windows-authentication iis-express

我正在尝试在我的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>
Run Code Online (Sandbox Code Playgroud)

更新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.
Run Code Online (Sandbox Code Playgroud)

对ApplicationHost.config

<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>
Run Code Online (Sandbox Code Playgroud)

mal*_*awi 12

使用IISExpress进行Windows身份验证

更新您的web.config

确保您的web.config文件都启用了Windows身份验证,并且还拒绝匿名身份验证.如果应用程序通过匿名身份验证,HttpContext.Current.User.Identity.Name将为空.您的配置应如下所示:

<authentication mode="Windows" />
<authorization>
    <deny users="?"/>
</authorization>
Run Code Online (Sandbox Code Playgroud)

错误401.2未经授权有时,您可能会收到错误HttpContext.Current.User.Identity.Name.如果这样做,请验证您是否有权根据您提供的凭据查看此目录或页面.还要确保在Web服务器上启用了身份验证方法.

更新applicationhost.config

您还可能会发现必须更新IISExpress applicationhost.config文件(不要担心 - 我也不知道).这基本上是IIS配置工具的文件版本,您可以在其中配置Web服务器本身.查找applicationhost.config文件可能很棘手.它可能在:

%USERPROFILE%\文件\ iisexpress \设置\对ApplicationHost.config

要么

%userprofile%\ my documents\iisexpress\config\applicationhost.config

找到后,更新以下行(特别注意enabled = true):

<windowsAuthentication enabled="true">
    <providers>
        <add value="Negotiate" />
        <add value="NTLM" />
    </providers>
</windowsAuthentication>
Run Code Online (Sandbox Code Playgroud)

这是文章

  • 在VS 2017中进行调试时,我发现需要更新[解决方案路径] \.vs\config\applicationhost.config.我将身份验证元素替换为: (9认同)

Mzn*_*Mzn 9

我们对几乎所有的Intranet应用程序(包括SharePoint)都使用Windows身份验证.如果员工的浏览器未自动将其Windows凭据自动发送到站点,则员工必须登录.

在IE上,这是浏览器配置的问题.我认为还有一些方法可以配置Chrome和Firefox自动发送Windows登录.我认为Chrome会像IE一样关注Window的互联网设置(在客户端上).尝试将用户身份验证选项设置为"使用当前用户名和密码自动登录".

请参阅下面的屏幕截图,了解其位置.

在此输入图像描述

另请注意,这涉及用户的浏览器将Windows令牌发送到应用程序.应用程序必须理解并信任此令牌的来源,这可以在用户和应用程序所在的"域"的支持下工作.我认为它可以在单个机器上工作(在调试时),但是如果您希望这在网络上的多台计算机上运行,​​则需要考虑创建域.创建域的典型方法是Active Directory.

让我知道.


daj*_*ajo 9

在 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> 
Run Code Online (Sandbox Code Playgroud)

更多信息:https : //stackoverflow.com/a/4813716/555142


Ant*_*yrd 3

我可以通过删除协商提供商来使其正常工作。

  <windowsAuthentication enabled="true">
    <providers>
      <add value="NTLM" />
    </providers>
  </windowsAuthentication>
Run Code Online (Sandbox Code Playgroud)