getting username logged in

  • Thread starter Thread starter Brian Henry
  • Start date Start date
B

Brian Henry

im wokring on an intranet site, and i need the username of the current user
logged in so it displays their information, how do i find out the user name
thats logged into the system so i can use it in asp.net?
 
Francis Shanahan said:
I assume you've authenticated with Forms authentication, if not just replace
Forms in this example with Generic.

If Not (HttpContext.Current.User Is Nothing) Then
If (HttpContext.Current.User.Identity.IsAuthenticated) Then
If (TypeOf HttpContext.Current.User.Identity Is
FormsIdentity) Then
Dim userIdentity As FormsIdentity =
CType(HttpContext.Current.User.Identity, FormsIdentity)

/////////Once you have the userIdentity you can get the username I think.
End If
End If
End If

End Sub

Actually, the following will work:

Dim UserName As String = Nothing
If Not (HttpContext.Current.User Is Nothing) AndAlso _
(HttpContext.Current.User.Identity.IsAuthenticated) Then
UserName = HttpContext.Current.User.Identity.Name
End If

If you place this code on a web form or user control, you can simply refer
to "Context" instead of "HttpContext.Current", and you can use
Request.IsAuthenticated.
 
Thanks! I had a problem at first, but turned out I forgot to turn on windows
authentication and turn off anonymous access in IIS... your code worked
great!
 
Back
Top