0

I want to check if user is disabled in Active Directory. I have written following code but it is not working.

private DataTable IsUserExists(DataTable dt)
    {
        DataTable temp = new DataTable();
        temp.Columns.Add("DisplayName", typeof(string));
        temp.Columns.Add("DOE", typeof(DateTime));

        try
        {
            using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
            {

                foreach (DataRow row in dt.Rows)
                {

                    UserPrincipal users = UserPrincipal.FindByIdentity(context, row["DisplayName"].ToString());

                    if (users != null && (((UserPrincipal)users).Enabled.Value == true))
                    {
                        temp.Rows.Add(row["DisplayName"].ToString(), row["DOE"].ToString()); 
                    }
                }
                return temp;
            }
        }
        catch (Exception)
        {

            return null;
        }

    }​
joym8
  • 4,014
  • 3
  • 50
  • 93

0 Answers0