Hi Vineet Batta here..
Consider a scenario where you want to write an assembly which contains methods that only certain type of users can call (domain\Administrators or a specific custom users account). So how can we control this within code and let the runtime enforce these security checks?
1: public Class MyApplication
3: // You want that this method should only be called by users who are in domain\SuperUser account.
4: // You want to restrict the access by runtime.
5: public void UpdateUserProfile(int empId)
7: -- code to implement feature.
System.Security.Permission.PrinicpalPermission class and the related System.Security.Permission.PrinicpalPermissionAttribute class will enable you to check active principal (aka USER account information) under which the runtime will execute the code. PrinicpalPermission can be used to demand that the identity of the active principal match this information.
PrinicpalPermission has three core properties :
One of the most used methods is PrinicpalPermission.Demand(). It does the heavy lifting for us and you will see it in action in a sec...
To use declarative RBAC demands, we must complete the following three steps:
2. A Try/Catch block to catch underprivileged access attempts and to report errors.
3. Annotate the method with PrinicpalPermission attribute to declare method's access requirements.
2: void main()
5: // Step 1.
8: //Step 2.
12: myApplication app = new myApplication();
14: // call the method for which RBS has be declared.
18: catch(exception error)
20: // Catch and display message. This is just an example. You
21: // should implement you own robust error handling.
22: MessageBox.Show(" You do not have access to run this function")
28: public class myApplication
31: public myApplication();
34: //Step 3.
35: [PrincipalPermission(SecurityAction.Demand, Role = @"domain\SuperUsers")]
36: public void updateUserProfile(int empid)
38: --- write code here that should only be executed by specific users.
In step3 you see that that the users in the role ="domain\SuperUsers'' only can only execute this method. If any one who is not part of this group tries to execute, the runtime will throw appropriate exception. Of course we always need to fail closed!!
In the next blog I will show how the Imperative approach can be used and then compare the advantages and disadvantages of the two schemes.