Настройка свойства logonHours для блокировки разблокировки учетных записей пользователей - PullRequest
1 голос
/ 18 августа 2011

Как мне установить каждый бит свойства logonHours (который равен 21 байту) в ноль (для блокировки учетной записи пользователя) или один (для разблокировки)? Я получил свойство logonHours в байтах следующим образом:

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

Ответы [ 2 ]

2 голосов
/ 31 августа 2011

// После поиска в Интернете я использовал следующий код:

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

}

0 голосов
/ 21 августа 2011

Вот статья, которая может вам помочь: Разрешенные Active Directory времена входа в систему с C #

...