Using C#, how do you check if a computer account is disabled in active directory?

后端 未结 8 885
暖寄归人
暖寄归人 2020-12-16 17:24

How do you check if a computer account is disabled in Active Directory using C#/.NET

8条回答
  •  爱一瞬间的悲伤
    2020-12-16 17:47

    hey i got it finallyy :) here is my code hope it helps you

    const int ADS_UF_ACCOUNTDISABLE = 0x00000002;

            DirectoryEntry de = new DirectoryEntry();
            de.Path = "LDAP://companyname.com";
            DirectorySearcher objADSearcher = new DirectorySearcher(de);
            de.AuthenticationType = AuthenticationTypes.Secure;
    
            objADSearcher.SearchRoot = de;
            objADSearcher.Filter = "(SAMAccountName=" + TextBox1.Text + ")";
            SearchResult results = objADSearcher.FindOne();
            if (results.ToString() !="")
            {
    
               int flags= Convert.ToInt32(results.Properties["userAccountControl"][0].ToString());
    

    //for reference results.Properties["userAccountControl"][0].ToString().Equals("514");

               if (Convert.ToBoolean(flags & ADS_UF_ACCOUNTDISABLE))
               {
                   Response.Write("Account Disabled");
               }
    

提交回复
热议问题