views:

223

answers:

3

I'm building a C# application, and I want to identify users by their username. For example, if I logged onto the domain mydomain as the user myusername I'd want to get the mydomain\myusername so I can identify the user.

How can I do this with C#?

+6  A: 

It's very simple indeed:

Environment.UserName

From the MSDN page:

The UserName property provides part of the credentials associated with computer domain names, as returned by the UserDomainName property. This information is typically represented as DOMAIN\username.

This property can be used to identify the current user to the system and application for security or access purposes. It can also be used to customize a particular application for each user.

You can also get the domain name using Environment.UserDomainName.

Noldorin
Environment.UserName doesn't return the domain with it.
Malfist
You want to prefix Environment.UserDomainName in that case.
Noldorin
+2  A: 

try User.Identity.Name or Environment.UserName.

EDIT:

Environment.UserDomainName - Domain

Environment.UserName - UserName

jinsungy
Environment.UserName doesn't return the domain with it.
Malfist
+1  A: 

Environment.UserDomainName will get the domain and Environment.UserName will get the username.

Timothy Carter