8

In my ASP.NET Application I get Informations from Active Directory. I must get Informations about a User with the GUID Informations (example: a28a6a34dsfdsf57d9e54f945a241) but I don't know how I can use the filter right for this search :/

for example if I search to a User Lastname:

DirectoryEntry Entry = new DirectoryEntry("LDAP://" + "Domain");

            string filter = "(&(objectClass=user)(objectCategory=person)(cn=" + txtBenutzer.Text + "*))";

            DirectorySearcher Searcher = new DirectorySearcher(Entry, filter);

            var q = from s in Searcher.FindAll().OfType<SearchResult>()
                    select new
                    {
                        //GetProperty(s, "objectGUID"),
                        Benutzer = GetProperty(s, "sAMAccountName"),
                        eMail = GetProperty(s, "mail"),
                        Vorname = GetProperty(s, "givenName"),
                        Nachname = GetProperty(s, "sn"),
                        Telefon = GetProperty(s, "telephoneNumber"),
                        UserID = s.GetDirectoryEntry().NativeGuid

                    };

            this.myListView.DataSource = q;
            this.myListView.DataBind();

now I need a filter with the GUID that I can find the one and only user in AD. The GUID for this Search I have in a string UserID = Session["UserID"].toString()

tarasov

Tarasov
  • 3,625
  • 19
  • 68
  • 128

1 Answers1

22

You don't need to search, you can bind directly to the object if you know the GUID, e.g.

var user = new DirectoryEntry("LDAP://<GUID=119d0d80-699d-4e81-8e4e-5477e22ac1b3>");

(replace with your actual ObjectGUID).

Check this MSDN entry: Using ObjectGUID to Bind to an Object

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
  • oh ok I test it tomorrow ;) I give you a request – Tarasov Jul 23 '12 at 17:36
  • @Tarasov: glad it helped :) If it works, you could accept the answer as well. Cheers. – Paolo Tedesco Jul 24 '12 at 08:10
  • I had a second error in my Code with UserID = s.GetDirectoryEntry().NativeGuid I must use UserID = s.GetDirectoryEntry().Guid^^ – Tarasov Jul 24 '12 at 08:15
  • To accept the answer there should be a mark below the vote display. For the difference about NativeGuid and Guid see http://stackoverflow.com/questions/1644989/difference-between-nativeguid-and-guid-in-active-directory – Paolo Tedesco Jul 24 '12 at 09:18