This articleArticleAs a supplement to another article: Introduction to. Net (C #) and Windows User Account Information Acquisition
When you get a windowsidentity object, how do you get its two identityreference: securityidentifier and ntaccount? First, the two subclasses can obtain a class, and the other can be obtained directly from the identityreference. Translate method. Different types of identityreference have different value attributes.
For the windowsidentity object, its user attribute returns the SID of the corresponding user, which is a securityidentifier object. In this way, ntaccount can also be obtained:
// + Using system. Security. Principal;
WindowsidentityWi= Windowsidentity.Getcurrent ();
// Full name
Console.Writeline (wi.User.Translate (Typeof(Ntaccount)).Value );
// Sid
Console.Writeline (wi.User.Value );
Another method is to use the name attribute of windowsidentity. This name attribute is not unique to windowsidentity, but is inherited from the iidentity interface. windowsidentity represents the entire process of the current user. In this process, you can create an ntaccount object and use the translate method to obtain the securityidentifier.
As follows:Code:
// + Using system. Security
// + Using system. Security. Principal;
IidentityII= Windowsidentity.Getcurrent ();
// Create an ntaccount
NtaccountNTACC= New Ntaccount(Ii.Name );
// Full name
Console.Writeline (NTACC.Value );
// Sid
Console.Writeline (NTACC.Translate (Typeof(Securityidentifier)).Value );
They will output the full process and Sid of the current user account, for example, my computer is like this:
Mgen-PC \ mgen
S-1-5-21-2376214308-3361272619-2153758801-1000