使用电子邮件查找 Active Directory 是否 Enabled/Disabled
Find if Active Directory is Enabled/Disabled with Email
我试图通过搜索用户的电子邮件地址并检查已启用 属性 来查找用户是否拥有 Active Directory 帐户(如果我 return 已启用为真 - 运行 代码,如果我 return false - 运行 其他代码,如果结果为空 - return false 因为该电子邮件不再存在)。当我进入 foreach 循环时,它根据 result 中的电子邮件找到了用户,但检查了 if 和 elses returns user 为 NULL。
public static bool DoesUserExist(string email, string domain)
{
var found = false;
using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain))
{
UserPrincipal user = new UserPrincipal(domainContext);
user.EmailAddress = email;
PrincipalSearcher search = new PrincipalSearcher(user);
search.QueryFilter = user;
PrincipalSearchResult<Principal> results = search.FindAll();
foreach (Principal result in results)
{
if (user.Enabled == true)
{
found = false;
Helpers.LogMessage("Active Directory Account is Enabled in " + domain + " domain");
}
else if (user.Enabled == false)
{
found = true;
Helpers.LogMessage("Active Directory User Account is Disabled in " + domain + " domain");
}
else if (user.Enabled == null)
{
found = true;
Helpers.LogMessage("No Active Directory Account Found in " + domain + " domain");
}
}
return found;
}
}
如果用户在 foreach 中启用或禁用,我缺少什么才能访问?
您的问题是用户与您的搜索结果或循环无关;它只是用于搜索的模板。另请注意,如果未找到结果,您将不会进入循环(results
将为空),因此测试 null
没有任何意义。另外,您的 found
设置似乎有误。
if (!results.Any())
Helpers.LogMessage("No Active Directory Account Found in " + domain + " domain");
else {
var found = false;
foreach (UserPrincipal result in results) {
found = !result.Enabled;
if (found)
Helpers.LogMessage("Active Directory User Account is Disabled in " + domain + " domain");
else
Helpers.LogMessage("Active Directory Account is Enabled in " + domain + " domain");
}
}
我试图通过搜索用户的电子邮件地址并检查已启用 属性 来查找用户是否拥有 Active Directory 帐户(如果我 return 已启用为真 - 运行 代码,如果我 return false - 运行 其他代码,如果结果为空 - return false 因为该电子邮件不再存在)。当我进入 foreach 循环时,它根据 result 中的电子邮件找到了用户,但检查了 if 和 elses returns user 为 NULL。
public static bool DoesUserExist(string email, string domain)
{
var found = false;
using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain))
{
UserPrincipal user = new UserPrincipal(domainContext);
user.EmailAddress = email;
PrincipalSearcher search = new PrincipalSearcher(user);
search.QueryFilter = user;
PrincipalSearchResult<Principal> results = search.FindAll();
foreach (Principal result in results)
{
if (user.Enabled == true)
{
found = false;
Helpers.LogMessage("Active Directory Account is Enabled in " + domain + " domain");
}
else if (user.Enabled == false)
{
found = true;
Helpers.LogMessage("Active Directory User Account is Disabled in " + domain + " domain");
}
else if (user.Enabled == null)
{
found = true;
Helpers.LogMessage("No Active Directory Account Found in " + domain + " domain");
}
}
return found;
}
}
如果用户在 foreach 中启用或禁用,我缺少什么才能访问?
您的问题是用户与您的搜索结果或循环无关;它只是用于搜索的模板。另请注意,如果未找到结果,您将不会进入循环(results
将为空),因此测试 null
没有任何意义。另外,您的 found
设置似乎有误。
if (!results.Any())
Helpers.LogMessage("No Active Directory Account Found in " + domain + " domain");
else {
var found = false;
foreach (UserPrincipal result in results) {
found = !result.Enabled;
if (found)
Helpers.LogMessage("Active Directory User Account is Disabled in " + domain + " domain");
else
Helpers.LogMessage("Active Directory Account is Enabled in " + domain + " domain");
}
}