How to get ou of an user

Discussion in 'ASP .Net Security' started by Guest, May 25, 2005.

  1. Guest

    Guest Guest

    Hi
    I wonder if there is other / faster ways to get ou of the login user?

    Here is what I can think of
    - authentication
    - Search with SAMAccountName=<username entered>
    - set PropertiesToLoad.Add("distinguishedName")
    - parse the OU= string from the property returned

    TIA

    --
     
    Guest, May 25, 2005
    #1
    1. Advertising

  2. That will probably work fine. You might also consider p/invoking the
    TranslateName Windows API as it can convert from login name to distinguished
    name.

    If you do parse, be careful about encoded "," characters in the DN. You
    can't be 100% sure that a simple Split on the comma will work as the name
    component can have a "," in it if it is preceded by "\". Thus you could
    have:

    CN=Doe\, Jane,OU=people,DC=domain,DC=com

    If your AD doesn't use that naming convention, you are fine, but just
    remember that it is legal syntax and could cause funny bugs at runtime in
    code that seems to work fine 95% of the time.

    Joe K.

    <dl> wrote in message news:%...
    > Hi
    > I wonder if there is other / faster ways to get ou of the login user?
    >
    > Here is what I can think of
    > - authentication
    > - Search with SAMAccountName=<username entered>
    > - set PropertiesToLoad.Add("distinguishedName")
    > - parse the OU= string from the property returned
    >
    > TIA
    >
    > --
    >
    >
    >
     
    Joe Kaplan \(MVP - ADSI\), May 25, 2005
    #2
    1. Advertising

  3. dl schrieb:

    > Here is what I can think of
    > - authentication
    > - Search with SAMAccountName=<username entered>
    > - set PropertiesToLoad.Add("distinguishedName")
    > - parse the OU= string from the property returned


    Why parsing the dn when you can just ask the corresponding class?

    If you search the account by using the DirectorySearcher class, as result
    you will get an object of type SearchResult. Call on this object
    ..GetDirectoryEntry().Parent.Path and you get what you want.

    Jan
     
    Jan Peter Stotz, May 25, 2005
    #3
  4. Yep, that works too. Thanks for pointing that out.

    Joe K.

    "Jan Peter Stotz" <> wrote in message
    news:...
    > dl schrieb:
    >
    >> Here is what I can think of
    >> - authentication
    >> - Search with SAMAccountName=<username entered>
    >> - set PropertiesToLoad.Add("distinguishedName")
    >> - parse the OU= string from the property returned

    >
    > Why parsing the dn when you can just ask the corresponding class?
    >
    > If you search the account by using the DirectorySearcher class, as result
    > you will get an object of type SearchResult. Call on this object
    > .GetDirectoryEntry().Parent.Path and you get what you want.
    >
    > Jan
     
    Joe Kaplan \(MVP - ADSI\), May 25, 2005
    #4
  5. Guest

    Guest Guest

    Thanks Joe.

    "Joe Kaplan (MVP - ADSI)" <> wrote
    in message news:...
    > That will probably work fine. You might also consider p/invoking the
    > TranslateName Windows API as it can convert from login name to

    distinguished
    > name.
    >
    > If you do parse, be careful about encoded "," characters in the DN. You
    > can't be 100% sure that a simple Split on the comma will work as the name
    > component can have a "," in it if it is preceded by "\". Thus you could
    > have:
    >
    > CN=Doe\, Jane,OU=people,DC=domain,DC=com
    >
    > If your AD doesn't use that naming convention, you are fine, but just
    > remember that it is legal syntax and could cause funny bugs at runtime in
    > code that seems to work fine 95% of the time.
    >
    > Joe K.
    >
    > <dl> wrote in message news:%...
    > > Hi
    > > I wonder if there is other / faster ways to get ou of the login user?
    > >
    > > Here is what I can think of
    > > - authentication
    > > - Search with SAMAccountName=<username entered>
    > > - set PropertiesToLoad.Add("distinguishedName")
    > > - parse the OU= string from the property returned
    > >
    > > TIA
    > >
    > > --
    > >
    > >
    > >

    >
    >
     
    Guest, May 26, 2005
    #5
  6. Guest

    Guest Guest

    Thanks Jan. But what is returned by the GetDirectoryEntry().Parent.Path?
    can you give a sample format please?
    TIA

    "Jan Peter Stotz" <> wrote in message
    news:...
    > dl schrieb:
    >
    > > Here is what I can think of
    > > - authentication
    > > - Search with SAMAccountName=<username entered>
    > > - set PropertiesToLoad.Add("distinguishedName")
    > > - parse the OU= string from the property returned

    >
    > Why parsing the dn when you can just ask the corresponding class?
    >
    > If you search the account by using the DirectorySearcher class, as result
    > you will get an object of type SearchResult. Call on this object
    > .GetDirectoryEntry().Parent.Path and you get what you want.
    >
    > Jan
     
    Guest, May 27, 2005
    #6
  7. dl wrote:

    > Thanks Jan. But what is returned by the GetDirectoryEntry().Parent.Path?
    > can you give a sample format please?


    The "DirectoryEntry.Path Property" is a String property. For a detailed
    description search the .Net Framwork SDK Documentation.

    VB.NET:

    Dim searcher as new DirectorySearcher("LDAP://dc=yourdomain,dc=com")
    searcher.Filter="(&(objectClass=user)(samaccountname=" & username & "))"
    Dim result as SearchResult = searcher.FindOne
    Dim userEntry as DirectoryEntry = result.GetDirectoryEntry()
    Dim containerEntry as DirectoryEntry = userEntry.Parent
    Dim String containerPath = containerEntry.Path

    Jan
     
    Jan Peter Stotz, May 27, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Rowton
    Replies:
    2
    Views:
    681
    Tom Rowton
    Aug 1, 2003
  2. Jaydeep
    Replies:
    3
    Views:
    1,064
    vedcyrus
    Jun 30, 2006
  3. Mike Newton
    Replies:
    0
    Views:
    2,922
    Mike Newton
    Jul 23, 2004
  4. Steve C. Orr [MVP, MCSD]
    Replies:
    0
    Views:
    1,631
    Steve C. Orr [MVP, MCSD]
    Jul 23, 2004
  5. =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=

    get user identity in user control

    =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=, Oct 25, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    5,248
    AlbertM.
    Apr 2, 2008
Loading...

Share This Page