如何检查Active Directory中是否存在该用户in。
我有LDAP String和UserID,我能找出该UserID是否存在于Active Directory中吗?我将此应用程序用于ASP.NET网络应用程序(.NET 3.5)
发布于 2010-12-16 04:44:07
您可以这样做(用域替换您要进行身份验证的域或完全删除参数):
public bool DoesUserExist(string userName)
{
using (var domainContext = new PrincipalContext(ContextType.Domain, "DOMAIN"))
{
using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userName))
{
return foundUser != null;
}
}
}
以实现对用户是否存在的检查。这来自System.DirectoryServices.AccountManagement
命名空间和程序集。
你可以在http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx上找到更多信息
您可能希望更多地了解PrincipalContext,因为它有一些有趣的方法来验证用户凭据等等。
发布于 2010-12-16 04:44:47
我将使用System.DirectoryServices.AccountManagement
名称空间。
string UserID = "grhm";
bool userExists = false;
using (var ctx = new PrincipalContext(ContextType.Domain))
{
using (var user = UserPrincipal.FindByIdentity(ctx, UserID))
{
if (user != null)
{
userExists = true;
user.Dispose();
}
}
}
有关更多信息,请参阅http://msdn.microsoft.com/en-us/library/bb344891.aspx
https://stackoverflow.com/questions/4453801
复制相似问题