How to: Determine a User's Login Name (Visual Basic)

You can use the My.User object to get information about the current user. This example shows how to use the My.User.Name property to get the user's login name.

An application uses Windows authentication by default, so the My.User returns the Windows information about the user who started the application.

Example

This example checks if the application uses Windows or custom authentication, and then uses that information to parse the My.User.Name property.

Function GetUserName() As String
    If TypeOf My.User.CurrentPrincipal Is 
      Security.Principal.WindowsPrincipal Then
        ' The application is using Windows authentication.
        ' The name format is DOMAIN\USERNAME.
        Dim parts() As String = Split(My.User.Name, "\")
        Dim username As String = parts(1)
        Return username
    Else
        ' The application is using custom authentication.
        Return My.User.Name
    End If
End Function

See Also

Tasks

How to: Determine the User's Domain (Visual Basic)

Walkthrough: Implementing Custom Authentication and Authorization (Visual Basic)

Reference

Name

Concepts

Accessing User Data (Visual Basic)