none
Converting Octet String to byte array for LogonHours attribute RRS feed

  • Question

  • Hi everyone,

    I'm attempting to set the Logon Hours attribute for Active Directory with C# but I'm running into problems.

    The Logon Hours attribute is bascially has an OctetString syntax. A typical value would be "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" (for all hours allowed). This in turn has to be converted into a byte array before it can be set as a Logon Hours attribute as shown below:

    string logonHours = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";

    byte[] hours = ToByteArray(logonHours);

    public static byte[] ToByteArray(string str)
    {
        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
        return encoding.GetBytes(str);
    }

    However, this simply fails. I've been trawling through the web for some sorta solution but nothing has been forthcoming. I'm basically trying to convert an Octet String into a byte array. Can anyone point me to the right direction in this? Thanks!

    Ryan
    Friday, February 11, 2011 2:43 PM

Answers

  • Hi Ryan,

     

    Could you please explain, how does it fail for you?  What error message are you receiving?  What are you expecting to receive?  Here is what I'm getting for hours after calling the ToByteArray function:

     

    -		hours	{byte[42]}	byte[]
    		[0]	70	byte
    		[1]	70	byte
    		[2]	70	byte
    		[3]	70	byte
    		[4]	70	byte
    		[5]	70	byte
    		[6]	70	byte
    		[7]	70	byte
    		[8]	70	byte
    		[9]	70	byte
    		[10]	70	byte
    		[11]	70	byte
    		[12]	70	byte
    		[13]	70	byte
    		[14]	70	byte
    		[15]	70	byte
    		[16]	70	byte
    		[17]	70	byte
    		[18]	70	byte
    		[19]	70	byte
    		[20]	70	byte
    		[21]	70	byte
    		[22]	70	byte
    		[23]	70	byte
    		[24]	70	byte
    		[25]	70	byte
    		[26]	70	byte
    		[27]	70	byte
    		[28]	70	byte
    		[29]	70	byte
    		[30]	70	byte
    		[31]	70	byte
    		[32]	70	byte
    		[33]	70	byte
    		[34]	70	byte
    		[35]	70	byte
    		[36]	70	byte
    		[37]	70	byte
    		[38]	70	byte
    		[39]	70	byte
    		[40]	70	byte
    		[41]	70	byte
    
    

     

    All the best,

     

    Roger

    • Marked as answer by eryang Monday, March 7, 2011 1:48 PM
    Friday, February 11, 2011 6:18 PM

All replies

  • Hi Ryan,

     

    Could you please explain, how does it fail for you?  What error message are you receiving?  What are you expecting to receive?  Here is what I'm getting for hours after calling the ToByteArray function:

     

    -		hours	{byte[42]}	byte[]
    		[0]	70	byte
    		[1]	70	byte
    		[2]	70	byte
    		[3]	70	byte
    		[4]	70	byte
    		[5]	70	byte
    		[6]	70	byte
    		[7]	70	byte
    		[8]	70	byte
    		[9]	70	byte
    		[10]	70	byte
    		[11]	70	byte
    		[12]	70	byte
    		[13]	70	byte
    		[14]	70	byte
    		[15]	70	byte
    		[16]	70	byte
    		[17]	70	byte
    		[18]	70	byte
    		[19]	70	byte
    		[20]	70	byte
    		[21]	70	byte
    		[22]	70	byte
    		[23]	70	byte
    		[24]	70	byte
    		[25]	70	byte
    		[26]	70	byte
    		[27]	70	byte
    		[28]	70	byte
    		[29]	70	byte
    		[30]	70	byte
    		[31]	70	byte
    		[32]	70	byte
    		[33]	70	byte
    		[34]	70	byte
    		[35]	70	byte
    		[36]	70	byte
    		[37]	70	byte
    		[38]	70	byte
    		[39]	70	byte
    		[40]	70	byte
    		[41]	70	byte
    
    

     

    All the best,

     

    Roger

    • Marked as answer by eryang Monday, March 7, 2011 1:48 PM
    Friday, February 11, 2011 6:18 PM
  • Not sure if this is any help but, here is how I would populate the logonhours attribute.

    Dim contextInfo As PrincipalContext = New PrincipalContext(ContextType.Domain, "contoso.com", "dc=contoso,dc=com")

    Dim userId as UserPrincipal = UserPrincipal.FindByIdentity(contextInfo,User.SamaccountName)

    Dim setVal as Byte = "00000000000000000000000000000000000000000" ' This denies all hours

    User.PermittedLogonTimes.SeValue(setVal,0) '0 is the array location, so you need to do this 20 more times or do some type of loop.

    User.PermittedLogonTimes.SeValue(setVal,1)'... to 20

    User.Save()

    'Note: the variable User is the userprincipal, where it says user.SamaccountName you need to specify the userid of the person as string.


    • Edited by AD-Z Tuesday, July 16, 2013 6:19 PM added more comments
    Tuesday, July 16, 2013 6:18 PM