Solved

ASP.NET get NT UserID

Posted on 2003-12-01
2
819 Views
Last Modified: 2013-11-25
Using ASP.Net how do I get the NT login ID of the user currently visiting my site?

I have done this before in the old ASP by setting the permissions on the directory to all domain users then using the Request.ServerVariables("REMOTE_USER")

What is the equivalent in .net

Thanks
0
Comment
Question by:cotaej
2 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 9850464
try
Environment.UserName

hongjun
0
 
LVL 6

Accepted Solution

by:
pillbug22 earned 100 total points
ID: 9850602
Here's a sub I call from the Page_Load to get all the user info you have/need for the page:



Page_Load(...)

      GetAccess()

End Sub


Private Sub GetAccess()

        ' ############### Grab Windows login name and store in session ###############
        If Session("userName") = "" Or IsDBNull(Session("userName")) Then
            If Page.User.Identity.IsAuthenticated Then
                Dim userName As Array = Split(Page.User.Identity.Name, "\", -1, CompareMethod.Text)
                Session.Add("userName", LCase(userName(1)))
            End If
        End If
        ' #############################################################



        ' ################# Grab user role/permissions from DB #####################
        ' Change this area to fit your app's user/security table (if present)
        Dim connection As SqlConnection = CreateConnection()
        Dim command As New SqlCommand("SELECT allowCreateModify, allowReview, admin FROM dbTable WHERE UsserName = '" & Session("userName") & "'", connection)
        Dim record As SqlDataReader = command.ExecuteReader()
        If (record.Read()) Then
            Session("allowCreateModify") = record.GetBoolean(0)
            Session("allowReview") = record.GetBoolean(1)
            Session("admin") = record.GetBoolean(2)
            Session("isPrivate") = True
        Else
            Session("isPrivate") = False
        End If
        connection.Close()
        command.Dispose()
        ' ###############################################################



        ' #################### Grab user info from Active Directory ####################
        Dim root As New DirectoryServices.DirectoryEntry("LDAP://DC=subDomain,DC=domian,DC=com")
        Dim rootSearch As New DirectorySearcher(root)
        Dim SearchResult As SearchResult
        Dim results As SearchResultCollection
        rootSearch.Filter = ("(samaccountname=" & Session("userName") & ")")
        results = rootSearch.FindAll

        For Each SearchResult In results
            ' If AD is filled in, just pull the seperate fields instead of parsing the CN
            Session("firstName") = ParseFirstName(SearchResult.Properties("CN").Item(0))
            Session("lastName") = ParseLastName(SearchResult.Properties("CN").Item(0))
            Exit For
        Next
        ' ###################################################################

    End Sub



    Private Function ParseFirstName(ByVal strCN As String) As String
        Dim nameArray
        nameArray = Split(strCN, " ", -1, 1)
        ParseFirstName = nameArray(1)
    End Function

    Function ParseLastName(ByVal strCN As String) As String
        Dim nameArray
        nameArray = Split(strCN, " ", -1, 1)
        ParseLastName = nameArray(0)
    End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now