我正在创建此项目以部署在我们公司的Intranet中。我正在使用此代码验证用户登录:
entry.Username = strUserName;
entry.Password = strPassword;
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(objectclass=user)";
try
{
searcher.FindOne();
return true;
}
它在我的localhost上运行良好,但是当我将其部署到Intranet时,我无法登录。
现在我的问题是,我可以通过Intranet访问目录吗?或者有更好的方法来实现这一目标吗?
答案 0 :(得分:1)
更简单的方法是使用System.DirectoryServices
和System.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 替换为您的密码。
答案 1 :(得分:0)
选中此Post
它使用Active Directory对Intranet上的站点成员进行身份验证。
答案 2 :(得分:0)
过去这对我很有用:
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