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

发布于 11-30 16:11 字数 184 浏览 0 评论 0原文

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

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

How do I set each bit of the logonHours property (which as 21 bytes) to either zero (for locking user account) or one (for unlocking) ? I have obtained the logonHours property in bytes as follows:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初2024-12-07 16:11:46

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

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();

   }

static class 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

}

//After seaching the web, I used the following code :

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();

   }

static class 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

}

孤者何惧2024-12-07 16:11:46

这是一篇可以帮助您的文章:使用 C# 的 Active Directory 允许登录时间

Here is an article that can help you : Active Directory Permitted Logon Times with C#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文