The namespace of the using is required:
Using System.Security.Principal;
To obtain the user to log on to the computer:
WindowsIdentity WindowsIdentity = WindowsIdentity.GetCurrent (); SecurityIdentifier sid = Windowsidentity.user;
Windowsidentity.name is the username of the user who landed the computer.
The resulting windowsidentity.name assumption is that the workgroup (Workgroup) is logged in. is the computer name \username; Assume that you are logged on to the domain. is the domain name \username.
I'm thinking of the question: if someone has the advantage of setting the computer name to the same name as the domain, is it possible to cheat some applications and forge them into a domain.So just use it.
Windowsidentity.name to infer whether to log on to the domain. is definitely not enough.
C # Tips: Get the user who is currently logged on to the computer (local user/domain user)