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 ());
}