Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET get NT UserID

Posted on 2003-12-01
2
Medium Priority
?
825 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 9850464
try
Environment.UserName

hongjun
0
 
LVL 6

Accepted Solution

by:
pillbug22 earned 400 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

688 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