C# ASP.NET Active Directory自动登录

C# ASP.NET Active Directory自动登录,c#,asp.net,authentication,iis,active-directory,C#,Asp.net,Authentication,Iis,Active Directory,我正在制作一个简单的网站来了解asp.net/AD身份验证 我使用了本教程中的一些代码片段:从登录页面成功地将AD与表单身份验证一起使用。我对网站使用以下IIS身份验证设置: Anonymous Authentication -Enabled ASP.NET Impersonation -Disabled Basic Authentication -Disabled Digest Authentication -Disabled Forms Authe

我正在制作一个简单的网站来了解asp.net/AD身份验证

我使用了本教程中的一些代码片段:从登录页面成功地将AD与表单身份验证一起使用。我对网站使用以下IIS身份验证设置:

Anonymous Authentication    -Enabled
ASP.NET Impersonation       -Disabled
Basic Authentication        -Disabled
Digest Authentication       -Disabled
Forms Authentication        -Enabled
Windows Authentication      -Disabled
我希望使用当前登录的windows用户的凭据,并且在失败时不提示或仅提示。当我将Web.config身份验证模式更改为“Windows”且IIS设置如下所示时,它会弹出一个凭据提示,但只会不断提示,并且从不接受凭据

Anonymous Authentication    -Enabled
ASP.NET Impersonation       -Disabled
Basic Authentication        -Disabled
Digest Authentication       -Disabled
Forms Authentication        -Disabled
Windows Authentication      -Enabled
我尝试过其他几种组合,但都失败了

本网站中的所有文件包括:

LdapAuthentication.cs - is in App_Code and is a direct copy/paste from the tutorial
Logon.aspx - is copy/pasted from the tutorial with the companies LDAP path added
Default.aspx - is a direct copy/paste from the WebForm1.aspx in the tutorial
Web.config (shown below)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms"> <!-- I also tried "Windows" -->
      <forms loginUrl="logon.aspx" name="adAuthCookie" timeout="10" path="/" />
    </authentication>
    <authorization>
      <deny users="?" />
      <allow users="*" />
    </authorization>
    <identity impersonate="true" />
    <anonymousIdentification enabled="false" />
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
</configuration>
ldapaauthentication.cs-在App_代码中,是本教程的直接复制/粘贴
Logon.aspx-从教程中复制/粘贴,并添加了公司LDAP路径
Default.aspx-是教程中WebForm1.aspx的直接复制/粘贴
Web.config(如下所示)

确保IIS正确配置为在表单中使用ActiveDirectory身份验证,它可以在Visual studio的本地服务器上工作,但不能在IIS中工作。 在IIS 7+中,它是应用程序池帐户。 -只需创建一个在该帐户下运行的新应用程序池,并将该应用程序池分配给您的应用程序/站点。 -右键单击新池(示例ASP.NET V4.0 Mypool)->高级设置 -在流程模型中,选择LocalSystem作为标识。 Web.config:

<system.web>
<compilation targetFramework="4.0" debug="true"/>
..........
<authentication mode="Forms">
      <forms loginUrl="login.aspx" name="adAuthCookie" timeout="10" path="/"/>
    </authentication>    
    <identity impersonate="false"/>
    <authorization>
      <deny users="?"/>
      <allow users="*"/>
    </authorization>
</system.web>

..........

确保IIS正确配置为在表单中使用ActiveDirectory身份验证,它可以在Visual studio的本地服务器上工作,但不能在IIS中工作。 在IIS 7+中,它是应用程序池帐户。 -只需创建一个在该帐户下运行的新应用程序池,并将该应用程序池分配给您的应用程序/站点。 -右键单击新池(示例ASP.NET V4.0 Mypool)->高级设置 -在流程模型中,选择LocalSystem作为标识。 Web.config:

<system.web>
<compilation targetFramework="4.0" debug="true"/>
..........
<authentication mode="Forms">
      <forms loginUrl="login.aspx" name="adAuthCookie" timeout="10" path="/"/>
    </authentication>    
    <identity impersonate="false"/>
    <authorization>
      <deny users="?"/>
      <allow users="*"/>
    </authorization>
</system.web>

..........

看看如何使用
PrincipalContext
以及非常少的
广告代码
我目前使用它,它是一个救命稻草,如果您只想进行自动登录或身份验证,允许用户在域中进入网页。。只需使用
PrincipalContext
非常简单,只需执行2到3行代码即可从web.config中删除表单身份验证元素,因为你不会在集成的Windows身份验证中使用它。看看使用
PrincipalContext
和很少的
AD code
我现在使用它,它是一个救命稻草,如果你只想进行自动登录或身份验证,允许用户在域中进入网页。。只需使用
PrincipalContext
非常简单,只需执行2到3行代码,即可从web.config中删除表单身份验证元素,因为在集成Windows身份验证中不会使用表单身份验证元素。