设置logonHours属性以锁定解锁用户帐户

时间:2011-08-18 18:10:36

标签: active-directory

如何将logonHours属性的每个位(21个字节)设置为零(用于锁定用户帐户)或一个(用于解锁)?我已经按字节获取了logonHours属性,如下所示:

bytes[] logonHours = (byte[])de.Properties["logonHours"].Value;

2 个答案:

答案 0 :(得分:2)

//搜索网页后,我使用了以下代码:

public void myMethod(String p_UserName)
  {
       byte[] logonHours = null;

       logonHours = (byte[])de.Properties["logonHours"].Value;
       BitArray bitArray = new BitArray(logonHours);

        for (int i = 0; i < bitArray.Count; i++)
        {
            //If bit is zero (that is false), logonHours is Locked
            //If bit is one (that is true) then set to zero (false) to LOCK the account
            if (bitArray[i])
            {
                bitArray.Set(i, false);
            }
        }

        byte[] m_Bytes1 = MyExtension.ConvertToByteArray(bitArray);


        de.Properties["logonHours"].Value = m_Bytes;
        de.CommitChanges();

   }

静态类MyExtension {

 public static byte[] ConvertToByteArray(this BitArray bits)
   {
       int numBytes = bits.Count / 8;
       if (bits.Count % 8 != 0) numBytes++;

       byte[] bytes = new byte[numBytes];
       int byteIndex = 0, bitIndex = 0;

       for (int i = 0; i < bits.Count; i++)
       {
           if (bits[i])
               bytes[byteIndex] |= (byte)(1 << (7 - bitIndex));

           bitIndex++;
           if (bitIndex == 8)
           {
               bitIndex = 0;
               byteIndex++;
           }
       }

       return bytes;
   }//end of method

}

答案 1 :(得分:0)

以下是一篇可以帮助您的文章:Active Directory Permitted Logon Times with C#