How can i get the
DomainName\AccountName
as string with the .NET Framework?
How can i get the
DomainName\AccountName
as string with the .NET Framework?
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
if you are using ASP.NET you can use
HttpContext.Current.User.Identity.Name;
System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString()
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.