Reading System.Byte[] Value from AD

P

Pere Raphael

Hi all,

I am writng some code to read AD user properties but I am not having much
luck reading octet values. For example, there is a property userPassword
which returns System.Byte[].

When I use ADSIEDIT, I can see the value in the format 0x077 0x043 0x064
0x055 0x072 0x054 0x44 0x058 which I can convert by using Character Map.

Does anyone know how I can get the value of this property.

Regards,

Pere

PS: I am very new to C#
 
D

Dave Sexton

Hi Pere,

System.Text.Encoding.Default.GetString(bytes);

You can replace "Default" with any of ASCII, UTF32, UTF8, UTF7, Unicode and
BigEndianUnicode if you'd like to match the appropriate encoding.

HTH
 
P

Pere Raphael

Hi Dave,

Thanks for your reply.
I tried the suggestion but I get an error message.
It's possible that I have not explained my problem clearly.
Please the relevant part of my code.
When I get the AD search result for user with "surname", I display the user's CN and userPassword.
Instead of being shown the password, I get System.Byte[].
Apparently adObject.Properties["userPassword"][0] is an object so can't convert to string.
Hope you're able to help.

Basicly trying to write code to email user's password to the user.

Thanks again,

Pere

DirectorySearcher adSearcherObject = new DirectorySearcher(adFolderObject);
adSearcherObject.SearchScope = SearchScope.Subtree;
Console.Write("Enter name to search for: ");
surname = Console.ReadLine();

adSearcherObject.Filter = "(&(ObjectClass=user)(sn=" + surname + "))";

foreach (SearchResult adObject in adSearcherObject.FindAll())
{
count++;
found = true;
Console.WriteLine("CN={0}", adObject.Properties["CN"][0]);
PasswordString = System.Text.Encoding.Default.GetString(adObject.Properties["userPassword"][0]);
Console.WriteLine(PasswordString);

Dave Sexton said:
Hi Pere,

System.Text.Encoding.Default.GetString(bytes);

You can replace "Default" with any of ASCII, UTF32, UTF8, UTF7, Unicode and
BigEndianUnicode if you'd like to match the appropriate encoding.

HTH

Pere Raphael said:
Hi all,

I am writng some code to read AD user properties but I am not having much
luck reading octet values. For example, there is a property userPassword
which returns System.Byte[].

When I use ADSIEDIT, I can see the value in the format 0x077 0x043 0x064
0x055 0x072 0x054 0x44 0x058 which I can convert by using Character Map.

Does anyone know how I can get the value of this property.

Regards,

Pere

PS: I am very new to C#
 
C

chanmm

Have you visit here?
http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx

chanmm
Hi Dave,

Thanks for your reply.
I tried the suggestion but I get an error message.
It's possible that I have not explained my problem clearly.
Please the relevant part of my code.
When I get the AD search result for user with "surname", I display the user's CN and userPassword.
Instead of being shown the password, I get System.Byte[].
Apparently adObject.Properties["userPassword"][0] is an object so can't convert to string.
Hope you're able to help.

Basicly trying to write code to email user's password to the user.

Thanks again,

Pere

DirectorySearcher adSearcherObject = new DirectorySearcher(adFolderObject);
adSearcherObject.SearchScope = SearchScope.Subtree;
Console.Write("Enter name to search for: ");
surname = Console.ReadLine();

adSearcherObject.Filter = "(&(ObjectClass=user)(sn=" + surname + "))";

foreach (SearchResult adObject in adSearcherObject.FindAll())
{
count++;
found = true;
Console.WriteLine("CN={0}", adObject.Properties["CN"][0]);
PasswordString = System.Text.Encoding.Default.GetString(adObject.Properties["userPassword"][0]);
Console.WriteLine(PasswordString);

Dave Sexton said:
Hi Pere,

System.Text.Encoding.Default.GetString(bytes);

You can replace "Default" with any of ASCII, UTF32, UTF8, UTF7, Unicode and
BigEndianUnicode if you'd like to match the appropriate encoding.

HTH

Pere Raphael said:
Hi all,

I am writng some code to read AD user properties but I am not having much
luck reading octet values. For example, there is a property userPassword
which returns System.Byte[].

When I use ADSIEDIT, I can see the value in the format 0x077 0x043 0x064
0x055 0x072 0x054 0x44 0x058 which I can convert by using Character Map.

Does anyone know how I can get the value of this property.

Regards,

Pere

PS: I am very new to C#
 
K

Kevin Yu [MSFT]

Hi Chanmm,

The User-Password attribute in AD is a write-only property. The password is
encrypted and stored. You cannot get decrypted text, instead, you can only
have this user reset password. Please check the following link for more
information about the User-Password attribute.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adschema/ad
schema/a_userpassword.asp

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top