Export (0) Print
Expand All

User Class

Provides access to the information about the current user.

System.Object
  Microsoft.VisualBasic.ApplicationServices.User
    Microsoft.VisualBasic.ApplicationServices.WebUser

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

[HostProtectionAttribute(SecurityAction.LinkDemand, Resources = HostProtectionResource.ExternalProcessMgmt)]
public class User

The User type exposes the following members.

  NameDescription
Public methodUserInitializes a new instance of the User class.
Top

  NameDescription
Public propertyCurrentPrincipalGets or sets the current principal (for role-based security).
Protected propertyInternalPrincipalGets or sets the principal object representing the current user.
Public propertyIsAuthenticatedGets a value that indicates whether the user has been authenticated.
Public propertyNameGets the name of the current user.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeWithWindowsUserSets the thread's current principal to the Windows user that started the application.
Public methodIsInRole(BuiltInRole)Determines whether the current user belongs to the specified role.
Public methodIsInRole(String)Determines whether the current user belongs to the specified role.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The properties and methods exposed by the My.User object provide access to the information about the current user. The meaning of "current user" differs slightly between Windows and Web applications. In a Windows application, the current user is the user who runs the application. In a Web application, the current user is the user who accesses the application.

The My.User property also provides access to the IPrincipal for the current user. A principal object represents the user's security context, including that user's identity and any roles to which the user belongs.

For Windows applications, this property provides the same functionality as the CurrentPrincipal property. For Web applications, this property provides the same functionality as the User property of the object returned by the Current property.

NoteNote

For Windows applications, only projects built on the Windows Application template initialize the My.User object by default. In all other Windows project types, you must initialize the My.User object by calling the InitializeWithWindowsUser method explicitly or by assigning a value to CurrentPrincipal.

NoteNote

The My.User object cannot report information about the current Windows user when run under Windows 95 and Windows 98 because those operating systems do not support the concept of a logged-on user. You must implement custom authentication to use the My.User object on those operating systems.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Resources. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft