gpt4 book ai didi

c# - 使用 DirectoryServices 的跨域身份验证

转载 作者:行者123 更新时间:2023-11-30 21:08:50 24 4
gpt4 key购买 nike

我需要为我的 Intranet Web 应用程序创建一种方法,该方法将使用 DirectoryServices 针对默认域或用户指定的域对用户进行身份验证。

在我的登录表单上,用户将能够以 "username""password"或 "domain\username ""密码"当用户与网络服务器位于同一域中并且非常简单时,可以使用第一种情况。我使用的代码是:

 string domain = "";
// Code to check if the username is in form of "domain\user" or "user"
string username = ParseUsername(username, out domain);
if(domain == "")
domain = defaultDomain;

PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, username, password);
bool IsAuthenticated = context.ValidateCredentials(username, password)

我将用户名和密码传递给 PrincipalContext 构造函数,以便在我尝试访问另一个域时绑定(bind)调用。

对于本地域,代码工作正常。但是,当我尝试检查通过用户名指定的另一个域时,我收到“无法联系服务器”错误。

我也尝试过使用不同的 ContextOptions,例如 ContextOptions.SimpleBindContextOptions.Negotiate,但我似乎总是得到相同的结果。

我需要实现它,因为该应用程序正在交付给具有单域或多域环境的各种客户。在“远程”域的情况下,还有什么我应该指定的吗?代码需要灵活,因为这将部署在各种环境中。

谢谢

编辑:我必须指出,我更喜欢使用 DirectoryServices.AccountManagementPrincipalContext 来做到这一点,以便利用其他它还提供了功能。

另外,我必须提到,对于我的测试,我的 Dev 机器在 10.0.0.* 网络上,我测试的第二个域在 10.0.1.* 上。我有一条路线,而且我可以使用 ldap 客户端成功连接,所以问题是为什么我无法通过我的 asp.net 应用程序连接到域。

最佳答案

我想出了这个问题的解决方案。

为了支持多个域,无论是在信任关系中还是在孤立的网络中,首先我在我的 web.config 中添加了一个 NameValueCollection 来列出域及其域 Controller 。

  <domains>
<add key="domain1" value="10.0.0.1"/>
<add key="domain2" value="10.0.1.11"/>
</domains>

(有关配置添加的更多信息,请参见 this so question)

然后下一步是以我在问题中提到的方式从用户的凭据中读取域。获得域后,我尝试从配置值中查找相应的域 Controller ,以获得正确的 LDAP 连接字符串。所以我的方法是这样的:

private string GetLDAPConnection(string a_Domain, string a_Username, string a_Password)
{
// Get the domain controller server for the specified domain
NameValueCollection domains = (NameValueCollection)ConfigurationManager.GetSection("domains");
string domainController = domains[a_Domain.ToLower()];

string ldapConn = string.Format("LDAP://{0}/rootDSE", domainController);

DirectoryEntry root = new DirectoryEntry(ldapConn, a_Username, a_Password);
string serverName = root.Properties["defaultNamingContext"].Value.ToString();
return string.Format("LDAP://{0}/{1}", domainController, serverName);
}

一旦我取回正确的连接字符串,我就会通过寻址正确的 LDAP 进行新调用以验证用户身份

    ...
string ldapConn = GetLDAPConnection(domain, username, a_Password);
DirectoryEntry entry = new DirectoryEntry(ldapConn, username, a_Password);

try
{
try
{
object obj = entry.NativeObject;
}
catch(DirectoryServicesCOMException comExc)
{
LogException(comExc);
return false;
}

DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = string.Format("(SAMAccountName={0})", username);
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();

从这一点开始,我还可以执行我想要的所有其他查询,例如用户的组成员资格等。

由于对远程域的调用需要绑定(bind)到用户,因此我使用“调用”用户凭据。这样用户就可以通过身份验证并将调用绑定(bind)到特定用户。此外,我还指定了一个“默认”域,以防用户在未指定域的情况下提供其凭据。

我没有设法做到这一点,但是我想使用 PrincipalContext,但从好的方面来说,这个解决方案也适用于旧的 .NET 2.0 应用程序。

我不确定这是解决问题的最佳方法,但它似乎在我们迄今为止执行的测试中有效。

关于c# - 使用 DirectoryServices 的跨域身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9362724/

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