Customize user security context

Source: Internet
Author: User

Using system;

Using system. Security;

Using system. Security. Principal;

Using system. text;

Using system. Collections. Specialized;

Namespace securitydll

{

///

/// Summary of class1.

Public class EMP

{Public EMP ()

{

// Perform permission check. Only the admin role can call

System. Security. permissions. principalpermission perm = new system. Security. permissions. principalpermission (null, roles. admin. tostring (), true );

Perm. Demand ();

}

// Business processing

Public int add (int A, int B)

{Return a + B ;}

}

Public class mycredentials: system. Security. Principal. iprincipal

{

# Region iprincipal Member

Private system. Security. Principal. genericidentity _ identity;

Private stringcollection _ roles = new stringcollection ();

Public mycredentials (string username, string [] roles)

{

// Here we can do some authentication code

_ Identity = new genericidentity (username, "AAA ");

If (null! = Roles) _ roles. addrange (roles );

}

Public system. Security. Principal. iidentity identity

{

Get {

// Todo: Add mycredentials. Identity getter.

Return _ identity;

}

}

Public bool isinrole (string role)

{

// Todo: Add mycredentials. isinrole

Return _ roles. Contains (role );

}

# Endregion

}

///

/// Role provided by the current application ///

Public Enum roles {admin, sales, guest }}

// The client has different results when calling components with different role identities.

Private void button#click (Object sender, system. eventargs E)

{

Securitydll. mycredentials c = new securitydll. mycredentials ("greystar", new string [] {"sales "});

System. Threading. thread. currentprincipal = C;

Securitydll. emp ee = new securitydll. EMP ();

MessageBox. Show (EE. Add (1,1). tostring ());

}

Private void button2_click (Object sender, system. eventargs e ){

Securitydll. mycredentials c = new securitydll. mycredentials ("greystar", new string [] {"admin "});

// Call with the correct role, everything is normal

System. Threading. thread. currentprincipal = C;

Securitydll. emp ee = new securitydll. EMP ();

MessageBox. Show (EE. Add (1,1). tostring ());

}

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.