我们如何检查用户ID是否存在于Active Directory中。
我有LDAP字符串和UserID,我能否找到该UserID是否存在于Active Directory中。 我将在ASP.NET Web应用程序(.NET 3.5)中使用此方法。
我们如何检查用户ID是否存在于Active Directory中。
我有LDAP字符串和UserID,我能否找到该UserID是否存在于Active Directory中。 我将在ASP.NET Web应用程序(.NET 3.5)中使用此方法。
您可以按照以下方式进行操作(将域替换为您正在进行身份验证的域或完全删除该参数):
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
命名空间。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/zh-cn/library/bb344891.aspx
。user
,但我从未看到声明 usr
。它们应该是同一个变量吗? - Brian J