Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Converting Octet String from Active Directory

Posted on 2006-10-30
3
3,673 Views
Last Modified: 2009-09-18
I am working with AD in C#.

I wrote a simple little app that allows people to search users in AD.

I have the objectSid (SID) coming back as a poperty, and I need to store that data in a database. Later in the app, I will want to search AD using that SID stored.

My problem is converting objectSid ( guessing it's an octec string) to a string value to store it and then converting it back when I need to search with it.

Anyone been down the road with AD and C#

Thanks,
0
Comment
Question by:scaldwe
3 Comments
 
LVL 10

Accepted Solution

by:
jnhorst earned 500 total points
ID: 17838984
I am thinking your user object should be castable to an instance of the IADsUser interface, which has a get_GUID method.  You might want to look at storing that and using it later to search with it.

John
0
 

Author Comment

by:scaldwe
ID: 17842854
Thank You for the reply,  this is what I was looking for ...

using the namespace "System.Security.Principal"

    private String ConvertSID(Byte[] sidBinary)
    {
        SecurityIdentifier SID = new SecurityIdentifier(sidBinary, 0);
        return SID.ToString();
    }

returns me a nice little Unique String I can search on ....

Here is the actual code block this applies to ... maybe this can help someone get their a little faster if they are tasked with this as well.

//Define Search String .. ex: "(&(objectClass=user)(sn=sp*)(objectCategory=person))"
        StringBuilder objS = new StrinBuilder(500);
        objS.Append("(&(objectClass=user)(sn=");
        objS.Append(txtSearchCriteria.Text.Trim());
        objS.Append("*)(objectCategory=person))");
       

        DirectorySearcher objSearcher = new DirectorySearcher("LDAP://yourdomain/");
        objSearcher.Filter = objS.ToString();
        objSearcher.PropertiesToLoad.Add("sn");
        objSearcher.PropertiesToLoad.Add("givenName");
        objSearcher.PropertiesToLoad.Add("objectSid"); //returns as an octet string so you must convert it.

        SearchResult objResults;
        SearchResultCollection objResultCollection = objSearcher.FindAll();

        //Now to grab the values I set to Load with the PopertiestoLoad method ...
        String firstName = (String)objResults.Properties["givenName"][0];
        String lastName = (String)objResults.Properties["sn"][0];
        String SID = ConvertSID((Byte[])objResults.Properties["objectSid"][0];

Hope this helps someone ....




0
 

Expert Comment

by:CareCentric
ID: 25369395
For anyone interested, I had to do the reverse of this: finding all users that belong to a group, even if they are listed as primary group member. (I excluded 513 which is the default "Domain Users" group).
if (sr.Properties["name"] == null) continue;
if (sr.Properties["name"].Count == 0) continue;
string name = (string)sr.Properties["name"][0];
string gSID = ConvertSID((Byte[])sr.Properties["objectSid"][0]);
string rSID = gSID.Substring(gSID.LastIndexOf('-') + 1);
int irSID = 0;
int.TryParse(rSID, out irSID);
ResultPropertyValueCollection member = sr.Properties["member"];
List<string> members = new List<string>();
foreach (string i in member)
{
  members.Add(i);
}
if (irSID == 0 || rSID != "513")
{
  DirectorySearcher dsearch2 = new DirectorySearcher();
  SearchResultCollection sResult2;
  dsearch2.PageSize = 500;
  dsearch2.SizeLimit = 500;
  dsearch2.ReferralChasing = ReferralChasingOption.All;
  dsearch2.SearchScope = SearchScope.Subtree;
  dsearch2.Filter = "(&(primaryGroupID=" + rSID + "))";
  dsearch2.SearchRoot = new DirectoryEntry("LDAP://DC=contoso,DC=com");
  sResult2 = dsearch2.FindAll();
  foreach (SearchResult sr2 in sResult2)
  {
    members.Add(sr2.Path.Substring(7));
  }
}

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question