ASP.NET Active Directory 自动登录
Posted
技术标签:
【中文标题】ASP.NET Active Directory 自动登录【英文标题】:ASP.NET Active Directory Auto-Login 【发布时间】:2015-09-11 21:50:50 【问题描述】:我正在制作一个简单的网站来学习 asp.net/AD 身份验证。
我使用了本教程中的一些代码 sn-ps:https://support.microsoft.com/en-us/kb/316748 从登录页面成功使用带有表单身份验证的 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>
【问题讨论】:
看看使用PrincipalContext
和很少的AD code
我目前使用它,它是一个救生员,如果您只想进行自动登录或身份验证以允许用户进入网页,如果他们在域中.. 只需使用 PrincipalContext
非常简单,只需执行 2 到 3 行代码
尝试从您的 web.config 中删除表单身份验证元素,因为您不会将其与集成 Windows 身份验证一起使用。
【参考方案1】:
确保 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>
【讨论】:
以上是关于ASP.NET Active Directory 自动登录的主要内容,如果未能解决你的问题,请参考以下文章
ASP.NET 表单身份验证和 Active Directory 模拟
没有密码的 ASP.Net Core 中的 Active Directory 授权
如何在 Active Directory 中获取用户的组? (c#, asp.net)
由 Azure Active Directory v1 保护的 asp.net 核心 webapi
从 ASP.NET 应用程序使用 Active Directory 时出现 DirectoryServicesCOMException (0x80072020)