在Intranet中使用Active Directory进行登录身份验证..可能的

本文关键字:登录 身份验证 Directory Intranet Active | 更新日期: 2023-09-27 18:23:47

我正在创建这个项目,以便部署在我们公司的内部网中。我使用此代码来验证用户登录:

entry.Username = strUserName;
entry.Password = strPassword;
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(objectclass=user)";
try
{
    searcher.FindOne();
    return true;
}

它在本地主机上运行良好,但当我将其部署到intranet时,我无法登录。

现在我的问题是,我可以通过内部网访问目录吗?或者有更好的方法来实现这一点吗?

在Intranet中使用Active Directory进行登录身份验证..可能的

一个更简单的方法是使用System.DirectoryServicesSystem.DirectoryServices.AccountManagement

在返回布尔值的函数中使用:

Dim context As PrincipalContext = New PrincipalContext(ContextType.Domain, domainName)
If context.ValidateCredentials(userAlias, userPassword, ContextOptions.Negotiate) Then
    Return True
Else
    Return False
End If

这个片段是用VB编写的,但你已经明白了。用您的域名替换domainName,用您的用户名替换userAlias和用您的密码替换userPassword

这在过去对我很有用:

var ldapConnectionString = "LDAP://servername/CN=Users,DC=domainname,DC=com";
using (var de = new DirectoryEntry(ldapConnectionString, username, password, AuthenticationTypes.Secure))
{
    if(de.NativeObject != null)
    {
        // user is valid ...
    }
}

您需要引用:System.DirectoryServices