User.Name Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets the name of the current user.

Namespace:   Microsoft.VisualBasic.ApplicationServices
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public string Name { get; }

Property Value

Type: System.String

String. The name of the current user.

You can use the My.User object to get information about the current user.

The principal that authenticates the user also controls the format of the user name. By default, an application uses Windows authentication, and the user name has the format DOMAIN\USERNAME. A custom implementation of the principal does not necessarily use the same format.

System_CAPS_noteNote

The My.User.Name property returns an empty string when run under Windows 95 and Windows 98 because those operating systems do not recognize user names.

System_CAPS_noteNote

The exact behavior of the My.User object depends on the type of the application and on the operating system on which the application runs. For more information, see the User class overview.

Project type

Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

This example checks if the application is using Windows or custom authentication, and 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

SecurityPermission

Describes a set of security permissions applied to code. Associated enumeration: ControlPrincipal.

.NET Framework
Available since 2.0
Return to top
Show: