Getting current login from Active Directory using C# code

前端 未结 5 1232
不思量自难忘°
不思量自难忘° 2020-12-23 23:08

How can I get the current user\'s login name from Windows Active Directory using C# code?

5条回答
  •  臣服心动
    2020-12-23 23:46

    Simply,

    string Name = new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).Identity.Name;
    

    OR

    string Name = System.Environment.UserName  
    

    OR

    string Name = Environment.GetEnvironmentVariable("USERNAME");
    

    OR

    string Name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    

    works :)

提交回复
热议问题