User.Name 属性

定义

获取当前用户的名称。

public:
 property System::String ^ Name { System::String ^ get(); };
public string Name { get; }
member this.Name : string
Public ReadOnly Property Name As String

属性值

String。 当前用户的名称。

示例

此示例检查应用程序是否使用 Windows 或自定义身份验证,并使用该信息分析 My.User.Name 属性。

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

注解

可以使用该 My.User 对象获取有关当前用户的信息。

对用户进行身份验证的主体还控制用户名的格式。 默认情况下,应用程序使用 Windows 身份验证,用户名的格式为 DOMAIN\USERNAME。 主体的自定义实现不一定使用相同的格式。

注释

对象的确切行为 My.User 取决于应用程序的类型和运行应用程序的操作系统。 有关详细信息,请参阅 User 类概述。

按项目类型提供可用性

项目类型 可用的
Windows 应用程序 Yes
类库 Yes
控制台应用程序 Yes
Windows 控件库 Yes
Web 控件库 Yes
Windows 服务 Yes
网站 Yes

适用于

另请参阅