Environment.UserName 属性

定义

获取与当前线程关联的人员的用户名。

public:
 static property System::String ^ UserName { System::String ^ get(); };
public static string UserName { get; }
static member UserName : string
Public Shared ReadOnly Property UserName As String

属性值

与当前线程关联的人员的用户名。

示例

以下示例显示启动当前线程的人员的用户名。

// Sample for the Environment.UserName property
using System;

class Sample
{
    public static void Main()
    {
    Console.WriteLine();
//  <-- Keep this information secure! -->
    Console.WriteLine("UserName: {0}", Environment.UserName);
    }
}
// Sample for the Environment.UserName property
open System

//  <-- Keep this information secure! -->
printfn $"\nUserName: {Environment.UserName}"
' Sample for the Environment.UserName property
Class Sample
   Public Shared Sub Main()
      Console.WriteLine()
      '  <-- Keep this information secure! -->
      Console.WriteLine("UserName: {0}", Environment.UserName)
   End Sub
End Class

注解

可以使用该 UserName 属性来标识当前线程上的用户、系统和应用程序,以实现安全或访问目的。 它还可用于自定义每个用户的特定应用程序。

在WindowsUserName属性包装对 Windows GetUserName 函数的调用。 用户的域帐户凭据的格式为用户的域名、“\”字符和用户名。 使用该 UserDomainName 属性获取用户的域名和 UserName 用于获取用户名的属性。

在 Unix 平台上,该 UserName 属性包装对函数的 getpwuid_r 调用。

如果 ASP.NET 应用程序在开发环境中运行,UserName 属性将返回当前用户的名称。 在发布的 ASP.NET 应用程序中,此属性返回应用程序池帐户的名称(如默认 AppPool)。

适用于

另请参阅