. The core of Role-based security verification is the principal and identity objects. The principal is responsible for role or group verification, the identifier object encapsulates information about the user or entity being verified. Role security verification supports authorization by generating entity information that can be used by the current thread. The entity is constructed with an associated identifier.
Public interface iprincipal
{
// Methods
Bool isinrole (string role );
// Properties
Iidentity identity {Get ;}
}
Public interface iidentity
{
// Properties
String authenticationtype {Get ;}
Bool isauthenticated {Get ;}
String name {Get ;}
}
The. NET Framework provides two sets of Principal/identity types: windowsprincipal/windowsidentity Based on Windows operating system accounts and genericprincipal/genericidentity for custom authentication.
The principal object is bound to the callcontext object in the application domain (appdomain). By default, the application domain automatically creates the genericprincipal/genericidentity object that adopts the default security policy, and the principal object reference is automatically copied from the creation thread to the callcontext of the new thread. We can use thread. currentprincipal to obtain information about the default principal and identity objects.
Console. writeline (thread. currentprincipal );
Console. writeline (thread. currentprincipal. Identity );
Next, we use windowsprincipal/windowsidentity for some simple verification, and to further understand the purpose of Role-based security verification.
Static void test ()
{
Console. writeline ("test ...");
}
Static void main (string [] ARGs)
{
Test ();
}
To modify the code above, the test () method must have the administrator privilege to be called.
1. We use the system. Security. permissions. principalpermissionattribute feature to add the role verification mark to the test method. Therefore, the user called (a user logged on to Windows operating system) must be a member of the "Administrators" group.
2. Set the subject and identity object of the thread in the main method. Windowsidentity. getcurrent () is used to obtain the identity object of the current Login User. If the login user belongs to the adminstrators group, the test () method is called normally. Otherwise, a securityexception exception is triggered (we can use windowsidentity. getanonymous () obtains the anonymous user identity object to trigger this exception ).
[Principalpermission (securityaction. Demand, role = "Administrators")]
Static void test ()
{
Console. writeline ("test ...");
}
Static void main (string [] ARGs)
{
Thread. currentprincipal = new windowsprincipal (windowsidentity. getcurrent ());
Test ();
}
We can also use other methods to do this.
1. Use the principalpermission object to replace principalpermissionattribute. Then we can use dynamic permission verification to write user names or role parameters to the configuration file.
2. Use appdomain. currentdomain. setthreadprincipal to set the subject object and thread. currentprincipal. We can also directly use appdomain. currentdomain. setprincipalpolicy (principalpolicy. windowsprincipal) to allow the system to automatically create windowsprincipal/windowsidentity using the current logon username.
Static void test ()
{
New principalpermission (null, "Administrators"). Demand ();
Console. writeline ("test ...");
}
Static void main (string [] ARGs)
{
Appdomain. currentdomain. setthreadprincipal (New windowsprincipal (windowsidentity. getcurrent ()));
Test ();
}
Or
Static void test ()
{
New principalpermission (null, "Administrators"). Demand ();
Console. writeline ("test ...");
}
Static void main (string [] ARGs)
{
Appdomain. currentdomain. setprincipalpolicy (principalpolicy. windowsprincipal );
Test ();
}
The above examples are based on role or group verification. Of course, we can also perform verification based on users.
[Principalpermission (securityaction. Demand, name = "yuyun // Q. yuyun")] New principalpermission ("yuyun/Q. yuyun", null). Demand ();
Of course, we can also use both role and name. Principalpermission objects are more flexible than principalpermissionattribute. We can use them to perform Union operations on Multiple permissions and perform XML Conversion.
The following code is rewritten using genericprincipal/genericidentity.
// [Principalpermission (securityaction. Demand, name = "Q. yuyun", role = "Admins")]
Static void test ()
{
New principalpermission (null, "Administrators"). Demand ();
Console. writeline ("test ...");
}
Static void main (string [] ARGs)
{
// Create a Custom User ID object.
Genericidentity identity = new genericidentity ("Q. yuyun ");
// Create a subject object and specify an array of roles.
String [] roles = new string [] {"Admins "};
Genericprincipal principal = new genericprincipal (identity, roles );
// Set the subject.
Appdomain. currentdomain. setthreadprincipal (principal );
Test ();
}
In addition to the above method, we do not want to trigger a securityexception in some cases. We want to provide another execution policy, so we can directly use Principal. inrole and identity. Name.
Static void test ()
{
If (thread. currentprincipal. isinrole ("Admins") & thread. currentprincipal. Identity. Name = "Q. Yuhen ")
{
Console. writeline ("test ...");
}
Else
{
Console. writeline ("You have no execution permission! ");
}
}
To sum up, there are three methods for role-based security checks.
1. Use imperative security checks. This method is mainly implemented through the principalpermission. Demand () method.
2. Use declarative security checks. Use the principalpermissionattribute feature to specify the role and user name required for running.
3. Access the principal object directly. Access the thread. currentprincipal attribute to obtain the current subject and identity object, and call its related methods and properties for further verification.
For details, refer to the msdn document. Role verification is mainly used in the ASP. NET environment. The rain marks will be further described in the subsequent blog.