I'm trying to verify all users in a table to make sure they still exist in AD. I modeled my code after the answer to this question. However, on the FindByIdentity call, it's giving me this error:
'The specified directory service attribute or value does not exist.'
Haven't been able to find any answer to this so far. What am I doing wrong?
private void btnVerifyContactList_Click(object sender, System.EventArgs e)
{
txtList.Clear();
List<string> lst = (List<string>)SQL.GetAllUserADIDs();
StringBuilder sb = new StringBuilder();
using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "us.company.com"))
{
foreach (string s in lst)
{
txtList.AppendText($"ADID: {s} ");
try
{
using (UserPrincipal foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, s))
{
if (foundUser == null)
{
txtList.AppendText($"01\r\n");
sb.AppendLine(s);
}
else
{
txtList.AppendText($"00\r\n");
}
}
}
catch (Exception)
{
txtList.AppendText($"02\r\n");
sb.AppendLine(s);
}
}
}
txtList.Text = sb.ToString();
}