The easiest approach is through the environment class:
string user = Environment.UserDomainName + "\\" + Environment.UserName;
There are also several ways to restrict access to a specific user (although role checking is more common).
Beyond the obvious
if (userName == "domain\\john") { }
You can also use the following attribute for the entire class or specific methods:
[PrincipalPermission(SecurityAction.Demand, Name = "domain\\john", Authenticated = true)] void MyMethod() { }
Which may be a little more reliable for low-level reusable methods.
Note that you can use both, checking for the user that if () as part of the normal program flow and attribute as protection for critical methods.
source share