Active Directory Access from a Web App

Discussion in 'ASP .Net Security' started by Jon Schneider, Aug 28, 2006.

  1. I am trying to access employee information from Active Directory from my ASP
    ..Net web application. I have tried several different methods and each fail.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load

    Dim user As MembershipUser = Membership.GetUser(User.Identity.Name)

    userName.Text = user.UserName
    emailAddress.Text = user.Email

    End Sub

    This method returns this error:

    Object reference not set to an instance of an object.
    at _Default.Page_Load(Object sender, EventArgs e)

    The other method I was investigating is using WindowsIdentity. I can access
    the SID but I do not know how to utilize the DirectoryEntry, or what to
    include to obtain the users email address.

    Any help would be greatly appreciated.

    --
    Software Development Manager
    GRG Inc.
    Maitland, Florida 32751
    www.grgce.com
     
    Jon Schneider, Aug 28, 2006
    #1
    1. Advertising

  2. Jon Schneider

    Joe Kaplan Guest

    If you want to use LDAP/System.DirectoryServices directly, I wrote a whole
    book about that stuff that might be useful to you (see link in the
    signature). There's a free chapter and code samples available for download
    if you want to try before you buy. :)

    Essentially, if you have the SID of the user, you can create a binding
    string for the DirectoryEntry like this:

    LDAP://<SID=S-1-5-20-xxxxx>

    Where the SID value shown there is the value you get from calling ToString
    on the SecurityIdentifier object. Once you have that, then getting the
    user's email is easy:

    string mail = (string) entry.Properties["mail"].Value;

    The main trick may be figuring out how to set up the security context
    properly in ASP.NET to access the directory. This is covered in detail in
    ch 8, and I've posted on numerous approaches to this problem in these
    newsgroups over the years the Google will find for you.

    Best of luck,

    Joe K.

    --
    Joe Kaplan-MS MVP Directory Services Programming
    Co-author of "The .NET Developer's Guide to Directory Services Programming"
    http://www.directoryprogramming.net
    --
    "Jon Schneider" <> wrote in message
    news:...
    >I am trying to access employee information from Active Directory from my
    >ASP
    > .Net web application. I have tried several different methods and each
    > fail.
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs)
    > Handles Me.Load
    >
    > Dim user As MembershipUser = Membership.GetUser(User.Identity.Name)
    >
    > userName.Text = user.UserName
    > emailAddress.Text = user.Email
    >
    > End Sub
    >
    > This method returns this error:
    >
    > Object reference not set to an instance of an object.
    > at _Default.Page_Load(Object sender, EventArgs e)
    >
    > The other method I was investigating is using WindowsIdentity. I can
    > access
    > the SID but I do not know how to utilize the DirectoryEntry, or what to
    > include to obtain the users email address.
    >
    > Any help would be greatly appreciated.
    >
    > --
    > Software Development Manager
    > GRG Inc.
    > Maitland, Florida 32751
    > www.grgce.com
     
    Joe Kaplan, Aug 28, 2006
    #2
    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. JonnyD
    Replies:
    16
    Views:
    776
    Paul Clement
    Oct 31, 2005
  2. Hvid Hat
    Replies:
    1
    Views:
    500
    Alexey Smirnov
    Mar 17, 2009
  3. ejcosta
    Replies:
    2
    Views:
    913
    Eurico Costa
    Oct 8, 2004
  4. Sergio Lera via .NET 247
    Replies:
    1
    Views:
    254
    Joe Kaplan \(MVP - ADSI\)
    Apr 6, 2005
  5. Nick Brown
    Replies:
    3
    Views:
    365
    Richard Conroy
    Mar 18, 2010
Loading...

Share This Page