Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Active Directory Authentication & User Details

Status
Not open for further replies.

fatcodeguy

Programmer
Feb 25, 2002
281
CA
Hi,

I'm writing a VB.NET Web application that will authenticate a user and get their details from Active Directory. I have a version that works, but only if the user is working from the machine where the application is running. Here's my code and Web config

Can someone help me please? All help greatly appreciated

(lblUserDetails is a Label, and the only UI component)
Code:
        Dim webUserName As String = User.Identity.Name
        Dim windowsUserName As String = Principal.WindowsIdentity.GetCurrent.Name

        lblUserDetails.Text = "Web User: " & webUserName & "<br/>"
        lblUserDetails.Text &= "Windows User: " & windowsUserName & "<br/>"

        'parse user identity name for doman and username
        '<DOMAIN> / <sAMAccountName>
        Dim userNameElements As String() = webUserName.Split("\")
        lblUserDetails.Text &= "Web Domain: " & userNameElements(0) & "<br/>"
        lblUserDetails.Text &= "Web Name: " & userNameElements(1) & "<br/>"

        'userNameElements(1) is the sAMAccountName attribute in AD
        Dim sAMAccountName As String
        sAMAccountName = userNameElements(1)

        'Connect to AD
        'logon to rootDSE
        Dim rootDSE As New DirectoryEntry("LDAP://rootDSE")
        'get default naming context doman
        Dim domain As String = rootDSE.Properties("DefaultNamingContext")(0)
        'connect to default naming context
        Dim directoryEntry As New DirectoryEntry("LDAP://" & domain)
        lblUserDetails.Text &= "LDAP User: " & directoryEntry.Username & "<br/>"

        'Connected to AD.
        'Seach AD
        Dim searcher As New DirectorySearcher(directoryEntry)
        Dim results As SearchResultCollection
        Dim result As System.DirectoryServices.SearchResult
        Dim propertyName As String
        Dim propertyValue As String

        'set searcher settings
        searcher.PageSize = 500
        searcher.CacheResults = False
        searcher.SearchScope = SearchScope.Subtree
        searcher.SizeLimit = 5000

        'set filter
        searcher.Filter = "(&(objectcategory=User)(sAMAccountName=" & sAMAccountName & "))"

        'get results
        results = searcher.FindAll
        lblUserDetails.Text &= "<br/><br/>"
        For Each result In results
            lblUserDetails.Text &= "USER PROPERTIES<br/>"
            lblUserDetails.Text &= "-------------------------------------------<br/>"
            For Each propertyName In result.Properties.PropertyNames
                propertyValue = result.Properties(propertyName).Item(0).ToString
                lblUserDetails.Text &= propertyName & " = " & propertyValue & "<br/>"
            Next
            lblUserDetails.Text &= "<br/><br/>"
        Next
        directoryEntry.Close()

Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>    
  <system.web>

    <compilation defaultLanguage="vb" debug="true" />

    <customErrors mode="RemoteOnly" />

    <authentication mode="Windows" /> 

    <authorization>
        <allow users="*" /> <!-- Allow all users -->
    </authorization>

    <trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" />

    <sessionState 
            mode="InProc"
            stateConnectionString="tcpip=127.0.0.1:42424"
            sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
            cookieless="false" 
            timeout="20" 
    />
    
    <globalization requestEncoding="utf-8" responseEncoding="utf-8" />
    
    <identity impersonate = "true" />

  </system.web>
</configuration>

Thanks!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top