LadyGail
asked on
Get User WINNT Logon Name?
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("XXXX 9999\", "")
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
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.
Dim userNameOnly As String = authUserName
userNameOnly = userNameOnly.Replace("XXXX
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
ASKER
Not really, it need the code to run in asp.net.
oh well sorry
)-;
bc
)-;
bc
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)
Environ("USERNAME")
(also, sometimes the username may be listed under the "USER" or "UNAME" environmental variables)
ASKER
Environ("USERNAME") writes--ASPNET to the browser.
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.
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.
ASKER
Thank you.
I got it to work.
I got it to work.
May I ask how? Using Environ()?
ASKER
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.
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.
ASKER
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("D096 0001\", "")
If Trim(userNameOnly) = Trim(DSPageData2.Tables("V iewUsers") .Rows(0).I tem("User1 ")) Then
If Trim(userNameOnly) = Trim(DSPageData2.Tables("V iewUsers") .Rows(0).I tem("User2 ")) Then
If Trim(userNameOnly) = Trim(DSPageData2.Tables("V iewUsers") .Rows(0).I tem("User3 ")) Then
If Trim(userNameOnly) = Trim(DSPageData2.Tables("V iewUsers") .Rows(0).I tem("User4 ")) Then
Else
Response.Redirect("../Erro rMessage/U nauthorize dAccess.as px")
End If
End If
End If
End If
Thank you.
LadyGail
Dim authUserName As String
Dim aspUserName As String
authUserName = User.Identity.Name
aspUserName = Principal.WindowsIdentity.
Dim userNameOnly As String = authUserName
userNameOnly = userNameOnly.Replace("D096
If Trim(userNameOnly) = Trim(DSPageData2.Tables("V
If Trim(userNameOnly) = Trim(DSPageData2.Tables("V
If Trim(userNameOnly) = Trim(DSPageData2.Tables("V
If Trim(userNameOnly) = Trim(DSPageData2.Tables("V
Else
Response.Redirect("../Erro
End If
End If
End If
End If
Thank you.
LadyGail
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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