Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What benefit does your code receive if you decorate it with attributes demanding specific Security permissions?

Tags:

.net

What benefit does your code receive if you decorate it with attributes demanding specific Security permissions?

like image 993
Ashok Bishnoi Avatar asked Aug 20 '10 11:08

Ashok Bishnoi


People also ask

Which of the following are features of link demands and their usage?

A link demand only checks the immediate caller (direct caller) of your code. That means it doesn't perform a stack walk. Linking occurs when your code is bound to a type reference, including function pointer references and method calls. A link demand can only be applied declaratively.

What is the difference between code access security and evidence?

CAS provides evidence-based security built on a layer above the security provided by the Windows operating system. While Windows is based on the permissions of the user, CAS is based on the evidence for the assembly.


1 Answers

Most security permissions are especially useful when building reusable libraries that are designed to run in partial trust. This way you can restrict access to certain functions when an calling assembly or AppDomain does not have the proper rights configured. For an application that runs in full trust, most security permissions are not that useful.

However, there is one attribute that I tend to use quite a lot and that is the PrincipalPermissionAttribute. When you decorate a class or function with this attribute, .NET will check on every access whether the current thread's principle has the proper rights. In other words, you can allow or deny access to that code based on the role of a user (role based security). Here is an example:

[PrincipalPermission(SecurityAction.Demand, Role = "Managers")]
public static void ShowSalaryForEmployee(Employee employee)
{
    // code here.
}

UPDATE 2017:

The answer above is completely outdated. I stopped using this particular attribute a years ago, because it uses a CLR built-in code weaving technique, that makes it really hard to do both unit and integration testing where you're not interested in specifically testing the security aspect of the code.

Instead, I found it much better to define my own attributes that declare permissions on operstions (typically defined by messages) and implement authorization in the infrastructural level (typically using decorator) instead of relying on code weaving.

like image 108
Steven Avatar answered Oct 17 '22 11:10

Steven