Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET get NT UserID

Posted on 2003-12-01
2
Medium Priority
?
830 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

927 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