. Net (C #): Interesting identityreference and windowsidentity

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.