How to: Determine a User's Login Name

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

Walkthrough: Implementing Custom Authentication and Authorization

Concepts

Accessing User Data

Reference

My.User.Name Property