OrElse
OrElse

Reputation: 9969

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

How can i get the

DomainName\AccountName

as string with the .NET Framework?

Upvotes: 12

Views: 40228

Answers (5)

codingbadger
codingbadger

Reputation: 44024

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

Upvotes: 22

Dirk Vollmar
Dirk Vollmar

Reputation: 176219

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

Upvotes: 10

Eric Johansson
Eric Johansson

Reputation: 582

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.

Upvotes: 2

Jens Granlund
Jens Granlund

Reputation: 5070

WindowsIdentity.GetCurrent().Name

Upvotes: 1

Fahad
Fahad

Reputation: 2933

if you are using ASP.NET you can use

HttpContext.Current.User.Identity.Name;

Upvotes: 1

Related Questions