Solved

ASP.NET get NT UserID

Posted on 2003-12-01
2
818 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

18 Experts available now in Live!

Get 1:1 Help Now