Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get the DomainName\AccountName with the .NET Framework?

How can i get the

DomainName\AccountName

as string with the .NET Framework?

like image 268
OrElse Avatar asked Apr 27 '10 07:04

OrElse


3 Answers

System.Security.Principal.WindowsIdentity.GetCurrent().Name;
like image 198
codingbadger Avatar answered Nov 08 '22 11:11

codingbadger


Environment.UserDomainName contains the domain/computer name that your account is joined to. Environment.UserName contains only the username. To get the result you're after, you need to concaternate the variables(Environment.UserDomainName & "\\" & Environment.UserName). This only works well in a local context though, if you use this code in a website, you'll get the account name that your application pool is running under. In asp.net, use HttpContext.Current.User.Identity.Name instead.

like image 2
Eric Johansson Avatar answered Nov 08 '22 11:11

Eric Johansson


You can use the Environment.UserDomainName property to retrieve the domain and Environment.UserName to retrieve the user name:

Dim domainAndUserName As String _
    = Environment.UserDomainName & "\\" & Environment.UserName
like image 10
Dirk Vollmar Avatar answered Nov 08 '22 11:11

Dirk Vollmar