Solved

ASP.NET get NT UserID

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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