How to display username of visitor to page

  • Thread starter Thread starter Fred
  • Start date Start date
F

Fred

I would like to display the username of the visitor top a web page. This is
assuming that the visitor is coming from within a winodws2000 Domain. I
have tried the following but it gives me the user ASPNET which I assume is
coming from the IIS server not the web client :

Imports System.Security.Principal

'Returns current user
Public Function CurrentUser()
Dim pos As Integer, fullname As String
fullname = System.Security.Principal.WindowsIdentity.GetCurrent().Name
'Just to get at the username after the 'domainname\' Should really use
Substring instead of Mid
pos = InStr(fullname, "\", CompareMethod.Text)
CurrentUser =
Mid(System.Security.Principal.WindowsIdentity.GetCurrent().Name, pos + 1)
End Function


'To test whether user is a member of a group
Private Function InGroup(ByVal grp As String) As Boolean
Dim id As WindowsIdentity
id = WindowsIdentity.GetCurrent()
Dim p As New WindowsPrincipal(id)
InGroup = p.IsInRole("YourDomainName\" + grp)
End Function

.....and a sample Sub Main()....

Sub Main()
dim msg as string
If InGroup("AppUsers") Then
Application.Run(New YourMainForm)
Else
msg = "You are not a registered user. Please go away."
MessageBox.Show(msg, "Invalid user", MessageBoxButtons.OK)
End If
End Sub
 
I have also tried "Environment.Username" which gives me ASPNET and
"System.Web.HttpContext.Current.User.Identity.Name.ToString()" which gives
me nothing at all.

Any help?
 
for this to work you need to turn off anonymous access in IIS settings for
your website
 
IIS Settings > website you want to enable on > properties > Directory
security tab > edit
uncheck anonymous access and turn integrated windows authentication
 
To clarify for anyone who has the same problem I used
System.Web.HttpContext.Current.User.Identity.Name.ToString() but had to turn
off IIS anonymous authentication per Brian Henry's response.

Fred
 
Back
Top