My.User.Name Property 

Gets the name of the current user.

' Usage
Dim value As String = My.User.Name
' Declaration
Public ReadOnly Property Name As String

Return Value

String. The name of the current user.

Remarks

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.

Note

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.

Note

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 My.User Object.

Tasks

The following table lists examples of tasks involving the My.User.Name property.

To See

Get the user's login name

How to: Determine a User's Login Name

Get the user's domain name, if the application uses Windows authentication

How to: Determine the User's Domain

Implement custom authentication

Walkthrough: Implementing Custom Authentication and Authorization

Example

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

Requirements

Namespace: Microsoft.VisualBasic.ApplicationServices

Class: User, WebUser

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

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

Permissions

The following permission may be necessary:

Permission Description

SecurityPermission

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

For more information, see Code Access Security and Requesting Permissions.

See Also

Tasks

How to: Determine a User's Login Name
How to: Determine the User's Domain
Walkthrough: Implementing Custom Authentication and Authorization

Reference

My.User Object
Microsoft.VisualBasic.ApplicationServices.User.Name