To study how to use Java to obtain the SID of the system, I found a piece of code on msdn. The CS code can be compiled using the. net2 SDK.

Source: Internet
Author: User

Namespace windowsidentitymembers {
Using system;
Using system. Security. Principal;

Class windowsidentitymembers
{
[Stathread]
Static void main (string [] ARGs)
{
// Retrieve the Windows Account token for the current user.
Intptr logontoken = logonuser ();

// Constructor implementations.
Intptrconstructor (logontoken );
Intptrstringconstructor (logontoken );
Intptrstringtypeconstructor (logontoken );
Intprtstringtypeboolconstructor (logontoken );

// Property implementations.
Useproperties (logontoken );

// Method implementations.
Getanonymoususer ();

Impersonateidentity (logontoken );

Console. writeline ("this sample completed successfully;" +
"Press enter to exit .");
Console. Readline ();
}

// Create a windowsidentity object for the user represented by
// Specified Windows Account token.
Private Static void intptrconstructor (intptr logontoken)
{
// Construct a windowsidentity object using the input account token.
Windowsidentity = new windowsidentity (logontoken );

Console. writeline ("created a Windows identity object named" +
Windowsidentity. Name + ".");
}

 
// Create a windowsidentity object for the user represented by
// Specified account token and authentication type.
Private Static void intptrstringconstructor (intptr logontoken)
{
// Construct a windowsidentity object using the input account token
// And the specified authentication type.
String authenticationtype = "windowsauthentication ";
Windowsidentity =
New windowsidentity (logontoken, authenticationtype );

Console. writeline ("created a Windows identity object named" +
Windowsidentity. Name + ".");
}

// Create a windowsidentity object for the user represented by
// Specified account token, authentication type, and Windows Account
// Type.
Private Static void intptrstringtypeconstructor (intptr logontoken)
{
// Construct a windowsidentity object using the input account token,
// And the specified authentication type, and Windows Account type.
String authenticationtype = "windowsauthentication ";
Windowsaccounttype guestaccount = windowsaccounttype. Guest;
Windowsidentity =
New windowsidentity (logontoken, authenticationtype, guestaccount );

Console. writeline ("created a Windows identity object named" +
Windowsidentity. Name + ".");
}

// Create a windowsidentity object for the user represented by
// Specified account token, authentication type, Windows Account type, and
// Boolean authentication flag.
Private Static void intprtstringtypeboolconstructor (intptr logontoken)
{
// Construct a windowsidentity object using the input account token,
// And the specified authentication type, Windows Account type, and
// Authentication flag.
String authenticationtype = "windowsauthentication ";
Windowsaccounttype guestaccount = windowsaccounttype. Guest;
Bool isauthenticated = true;
Windowsidentity = new windowsidentity (
Logontoken, authenticationtype, guestaccount, isauthenticated );

Console. writeline ("created a Windows identity object named" +
Windowsidentity. Name + ".");
}

// Access the properties of a windowsidentity object.
Private Static void useproperties (intptr logontoken)
{
Windowsidentity = new windowsidentity (logontoken );
String propertydescription = "the Windows identity named ";

// Retrieve the Windows logon name from the Windows identity object.
Propertydescription + = windowsidentity. Name;

// Verify that the user account is not considered to be an anonymous
// Account by the system.
If (! Windowsidentity. isanonymous)
{
Propertydescription + = "is not an anonymous account ";
}

// Verify that the user account has been authenticated by windows.
If (windowsidentity. isauthenticated)
{
Propertydescription + = ", is authenticated ";
}

// Verify that the user account is considered to be a system account
// By the system.
If (windowsidentity. issystem)
{
Propertydescription + = ", is a system account ";
}

// Verify that the user account is considered to be a guest account
// By the system.
If (windowsidentity. isguest)
{
Propertydescription + = ", is a guest account ";
}

// Retrieve the authentication type for
String authenticationtype = windowsidentity. authenticationtype;

// Append the authenication type to the output message.
If (authenticationtype! = NULL)
{
Propertydescription + = ("and uses" + authenticationtype );
Propertydescription + = ("authentication type .");
}

Console. writeline (propertydescription );

// Display the SID for the owner.
Console. Write ("the SID for the owner is :");
Securityidentifier Si = windowsidentity. owner;
Console. writeline (Si. tostring ());
// Display the SIDS for the groups the current user belongs.
Console. writeline ("display the SIDS for the groups the current user belongs .");
Identityreferencecollection IRC = windowsidentity. Groups;
Foreach (identityreference IR in IRC)
Console. writeline (IR. value );
Tokenimpersonationlevel token = windowsidentity. impersonationlevel;
Console. writeline ("the impersonation level for the current user is:" + token. tostring ());
}

// Retrieve the account token from the current windowsidentity object
// Instead of calling the unmanaged logonuser method in the advapi32.dll.
Private Static intptr logonuser ()
{
String _ sectoken = windowsidentity. getcurrent (). User. value;
Console. writeline ("windowsidentity. getcurrent (). User. Value:" + _ sectoken );

Intptr accounttoken = windowsidentity. getcurrent (). Token;
Console. writeline ("token number is:" + accounttoken. tostring ());

Return accounttoken;
}

// Get the windowsidentity object for an anonymous user.
Private Static void getanonymoususer ()
{
// Retrieve a windowsidentity object that represents an anonymous
// Windows user.
Windowsidentity = windowsidentity. getanonymous ();
}

// Impersonate a Windows identity.
Private Static void impersonateidentity (intptr logontoken)
{
// Retrieve the Windows identity using the specified token.
Windowsidentity = new windowsidentity (logontoken );

// Create a windowsimpersonationcontext object by impersonating
// Windows identity.
Windowsimpersonationcontext impersonationcontext =
Windowsidentity. Impersonate ();

Console. writeline ("Name of the identity after impersonation :"
+ Windowsidentity. getcurrent (). Name + ".");
Console. writeline (windowsidentity. impersonationlevel );
// Stop impersonating the user.
Impersonationcontext. Undo ();

// Check the identity name.
Console. Write ("Name of the identity after discovery Ming an undo on ");
Console. writeline ("impersonation:" +
Windowsidentity. getcurrent (). Name );
}
}
}

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.