1
如何將logonHours屬性(它爲21個字節)的每一位設置爲零(用於鎖定用戶帳戶)或一個(用於解鎖)?設置logonHours屬性來鎖定解鎖用戶帳戶
bytes[] logonHours = (byte[])de.Properties["logonHours"].Value;
如何將logonHours屬性(它爲21個字節)的每一位設置爲零(用於鎖定用戶帳戶)或一個(用於解鎖)?設置logonHours屬性來鎖定解鎖用戶帳戶
bytes[] logonHours = (byte[])de.Properties["logonHours"].Value;
// seaching網頁後,我用下面的代碼:
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
}
下面是一篇文章,可以幫助你:我已經得到如下的logonHours屬性以字節爲單位Active Directory Permitted Logon Times with C#