在ASP.NET 3.5应用程序中使用Active Directory/LDAP登录用户
本文关键字:Directory Active LDAP 用户 登录 NET ASP 应用程序 | 更新日期: 2023-09-27 18:26:56
我必须找到一种方法,如何使用公司Active Direcotrys登录用户,我不确定是否有某种方法可以在没有管理员凭据(匿名)的情况下进行连接-我只需要在用户存在的情况下搜索他。
我尝试了很多解决方案,但都没有奏效。我是AD和ASP.NET的新手(但我以前用Java编程)。
到目前为止,我使用的代码来自MSDN站点:
DirectoryEntry entry = new DirectoryEntry(_path);
try
{
//Bind to the native AdsObject to force authentication.
object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(sAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (string)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
其中路径是可变的,链接到LDAP(LDAP:///rootDSE)
主要问题是,如果我正确连接到AD,我就无法跟踪。结果以null结尾,但我确信我使用了正确的凭据进行测试。
有人能帮吗
非常感谢
您可以查看
PrincipalContext
像这样:
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, myDomainTextBox.Text))
{
// validate the credentials
bool cIsValid = pc.ValidateCredentials(myUserNameTextBox.Text, myPasswordBox.Password);
if (cIsValid)
{
// Do some stuff
}
}