我想检查用户是否在Active Directory中禁用

时间:2016-11-02 19:26:40

标签: c# active-directory ldap principalcontext

我想检查Active Directory中是否禁用了用户。我写了下面的代码,但它不起作用。

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;
        }

    }​

0 个答案:

没有答案