Solved

Get User WINNT Logon Name?

Posted on 2004-04-23
12
328 Views
Last Modified: 2010-04-11
I am writing an Intranet app and need to get the user WINNT login name to allow access to the application.  I am using ASP.NET to write the app.  How do I capture the users WINNT logon name?
Thus far I have the following and it is not working:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        Dim authUserName As String
        Dim aspUserName As String
        authUserName = User.Identity.Name
        aspUserName = Principal.WindowsIdentity.GetCurrent.Name
        Dim userNameOnly As String = authUserName
        userNameOnly = userNameOnly.Replace("XXXX9999\", "")

       
         If authUserName = "XXXX9999\ABC" Then
            ///*** Allow user to view page****\\\
        Else
            ///*** Display message not allowed to view page****\\\
           
        End If

    End Sub
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Thank you.

LadyGail
0
Comment
Question by:LadyGail
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 5

Expert Comment

by:bitter_chicken
Comment Utility
Hey ladygail
I've used this code before, but it is VB6 - I have absolutely NO IDEA of the differences between VB6  <> VB.NET so I'll post it here unchanged, hope it helps.

----------------------------------------------------------------------------------------------

Option Explicit
'*******************************************************************
'* Project Location = C:\PROJECTX
'* This code is copyright (c), 1999 Andrea VB Programming News Group
'*******************************************************************
'************** API's
' Used to Get the logged on User Name - see GetPCUser Function
Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

'**********************
'* Public Variables etc
Public FileName As String   '* Hold the loaded project name
'* You can use an array but I found it better in this case to use the collection
Public LineHold1 As New Collection   '* collections to hold parsed information from .vbp file
Public LineHold2 As New Collection
'* Microsoft Word Object - Use Project on the menu then select
'* References then select Microsoft Office 9.0 Objects I have Office 2000
'* for Word 97 use 8.0 objects

Public Function GetPCUser() As String
Dim szBuffer As String, lBuffSize As Long, RetVal As Boolean

'* Create the buffer
szBuffer = Space(255)

lBuffSize = Len(szBuffer)

RetVal = GetUserName(szBuffer, lBuffSize)
If RetVal Then
    '* Strip off the null character on the User name
    GetPCUser = Left$(szBuffer, InStr(szBuffer, vbNullChar) - 1)
   
Else
    '* nope nothing there??
     GetPCUser = "UnknownUser"
End If
End Function

0
 

Author Comment

by:LadyGail
Comment Utility
Not really, it need the code to run in asp.net.
0
 
LVL 5

Expert Comment

by:bitter_chicken
Comment Utility
oh well sorry

)-;

bc
0
 
LVL 14

Expert Comment

by:JohnK813
Comment Utility
Don't know if you can access environmental variables in ASP, but you can give this a shot:

Environ("USERNAME")

(also, sometimes the username may be listed under the "USER" or "UNAME" environmental variables)
0
 

Author Comment

by:LadyGail
Comment Utility
Environ("USERNAME") writes--ASPNET to the browser.
0
Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

 
LVL 14

Expert Comment

by:JohnK813
Comment Utility
OK, that's probably because it's looking at the USERNAME on your server (and ASPNET is considered the user here), not the USERNAME of the computer accessing your page.

Hmmm... I don't know how to do this, sorry.  You may want to look into capturing the IP address or hostname, and converting that into a username (assuming each person has their own computer and only use that computer).

Another idea would be to use a cookie - the user enters their username once, and it's stored for future use.
0
 

Author Comment

by:LadyGail
Comment Utility
Thank you.
I got it to work.
0
 
LVL 14

Expert Comment

by:JohnK813
Comment Utility
May I ask how?  Using Environ()?
0
 

Author Comment

by:LadyGail
Comment Utility
Initially, the code I posted was shared with me by a co-worker.
However, I was getting errors using his code as posted above.
At this suggestion I looked through ALL of the files in his project to determine which file would result in the script functioning properly. Thus I happened to the file(s) that made the above script work.  Which file, I don't know.
But it works.
0
 

Author Comment

by:LadyGail
Comment Utility
My working solutions is listed:

            Dim authUserName As String
            Dim aspUserName As String
            authUserName = User.Identity.Name
            aspUserName = Principal.WindowsIdentity.GetCurrent.Name
            Dim userNameOnly As String = authUserName
            userNameOnly = userNameOnly.Replace("D0960001\", "")

            If Trim(userNameOnly) = Trim(DSPageData2.Tables("ViewUsers").Rows(0).Item("User1")) Then
                If Trim(userNameOnly) = Trim(DSPageData2.Tables("ViewUsers").Rows(0).Item("User2")) Then
                    If Trim(userNameOnly) = Trim(DSPageData2.Tables("ViewUsers").Rows(0).Item("User3")) Then
                        If Trim(userNameOnly) = Trim(DSPageData2.Tables("ViewUsers").Rows(0).Item("User4")) Then
                        Else
                            Response.Redirect("../ErrorMessage/UnauthorizedAccess.aspx")
                        End If
                    End If
            End If
        End If

Thank you.

LadyGail

0
 

Accepted Solution

by:
modulo earned 0 total points
Comment Utility
Closed, 125 points refunded.

modulo
Community Support Moderator
Experts Exchange
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

13 Experts available now in Live!

Get 1:1 Help Now