Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obtaining win/nt logon userid in vbscript

Status
Not open for further replies.

MED

Programmer
Aug 13, 2001
4
0
0
GB
Hello all, any ideas on how to get the logon userid using vbscript?? I'm able to do this in vb but i wonder if vbscript allow me to do this... I need this to filter users based on logon userid... i don't have an IIS server and ASP.
 
Are you trying to do this on a web page, ie, on the client. If so, then good luck, I can't help you. I think this is a security feature.<br>
<br>
If you were using ASP, you could look at Request.ServerVariables("REMOTE_USER") to get the user's login name, whatever he logged into your site with, anyway. Note that you can do this only on the server.<br>
<br>
_JoseC
 
The WSHNetwork object might help.<br>
Example:<br>
<br>
Dim WSHNetwork<br>
Set WSHNetwork = WScript.CreateObject("WScript.Network")<br>
<br>
WScript.Echo(WSHNetwork.UserDomain & "\" & _ WSHNetwork.UserName)<br>

 
With vbscript using ASP and IIS you can do it several ways:

User = Request.ServerVariables(&quot;REMOTE_USER&quot;)
or
User = Request.ServerVariables(&quot;AUTH_USER&quot;)

Both with return the username logged in with NT Challenge/Response.Authentication.
 
Hi all, thank you for your replies.
 
Another possibility is:
Code:
User = Request.ServerVariables(&quot;LOGON_USER&quot;)

If it doesn't return any username, put in the following code before the one above:
Code:
If Request(&quot;LOGON_USER&quot;) = &quot;&quot; Then
   Response.Status = &quot;401 Unauthorized&quot;
End if
 
This apparently won't help with med's problem since he says he's not using IIS, and though I haven't used REMOTE_USER or AUTH_USER, I have used LOGON_USER and it does return the login; you just have to pull it out of the string.

This is how I do it:

Code:
'*****************************************************************************
'*  This function passes the Logon_User ServerVariable to the parse function
'*  which extracts the NT loginID of the current user.  That value is then
'*  returned in the psLoginID variable.
'*****************************************************************************
Function GetUserLogon

	On Error Resume Next

	GetUserLogon = ParseUserFromDomain(Request.ServerVariables(&quot;LOGON_USER&quot;))
	
End Function
'****************************************************************************
'*	This function obtains the user's logon account name from the full
'*	domain name (with the logon name).  The logon name should be the string
'*	that appears after the last &quot;\&quot; found.
'****************************************************************************
Function ParseUserFromDomain(ByVal psLogonUser)

	Dim nPosFound
	Dim nPosOfLastSlash
	Dim nInt

	nPosOfLastSlash = 0 

	For nInt = 1 to Len(psLogonUser)
		nPosFound = InStr(nInt,psLogonUser, &quot;\&quot;)
		If nPosFound > 0 Then
			nPosOfLastSlash = nPosFound
		End If
	Next

	If nPosOfLastSlash = 0 Then
		'Look for forward slash
		For nInt = 1 to Len(psLogonUser)
			nPosFound = InStr(nInt,psLogonUser, &quot;/&quot;)
			If nPosFound > 0 Then
				nPosOfLastSlash = nPosFound
			End If
		Next
		If nPosOfLastSlash = 0 Then
			ParseUserFromDomain = &quot;Unknown&quot;
			Exit Function
		End If
	End If

	ParseUserFromDomain = Trim(Mid(psLogonUser, (nPosOfLastSlash + 1), Len(psLogonUser)))

End Function
 
Here's another, quicker and smaller, way to extract the users logon name from the entire domain name:

Code:
Function StripDomain(strLogonUser)
   strLogonUser = Replace(strLogonUser,&quot;/&quot;,&quot;\&quot;)
   StripDomain = Right(strLogonUser, (Len(strLogonUser) - InStrRev(strLogonUser, &quot;\&quot;, -1, vbTextCompare)))
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top