Click here to Skip to main content
Email Password   helpLost your password?

Introduction

This article describes an easy, manageable and light way to secure methods in your application.

Background

Lately, I have been developing an application, some parts (methods) of which needed to be secured and available only for some users. The requirement was also that the model should be very flexible and portable so it can be used in stand-alone and Web applications.

Other Solutions

After some searching, I have found two solutions that have partially been solving my problem.

.NET Framework Built In Security

.NET Framework offers the PrincipalPermission class that can solve this problem:

public class MyClass
{
    [PrincipalPermission(SecurityAction.Demand, Name = “Comp1\Administrator”)]
    public void MyMethod()
    {
  
    }
}

When a user that is not an Administrator will try to access this method, an exception will be thrown. Attributes itself are not called before every method by default. They are called during reflection of a given assembly and this is done here by the .NET Framework.

This works fine but there are some limitations:

  1. Security group (in our case Comp1\Administrator) must be hard coded and cannot be set dynamically.
  2. You have to use Microsoft authentication which might be a problem for, let’s say, Mono users.

Imperative Call

This brings us to the second solution. You can also do it in a basic manner by imperatively calling a security check inside a method:

public class MyClass
{
    public void MyMethod()
    {
        if(!IsPermited(MethodInfo.GetCurrentMethod(), User))
        {
            throw new NotAuthorizedException();
        }
    }
}

IsPermited can take authorization values written inside a database and authorize the user to use (or not) this MyMethod. The table with security values can look like this:

Id Method_Name User_Id
1 MyClass.MyMethod 1

The problem with this solution is that it is very hard to manage since the names of the secured parts of your application are not strongly-typed. For example, if you want to add permission for a new user, you have to put “by hand” name of your secured method. Those values can be kept inside another DB table or in the configuration file, but it can still create desynchronization with data in your application so it is a potential source of errors.

Hybrid Solution

The third solution was to combine the above two solutions and create a “Hybrid”.

We will leave imperative call of the security method but the class and method in which it is called must be “signed” by our specific attribute. That attribute checked during the call of the IsPermited method and if the call comes from “not signed” method, an exception is raised.

So now MyMethod should look like this:

public class MyClass
{
    [MethodSecured]
    public void MyMethod()
    {
        if(!IsPermited(MethodInfo.GetCurrentMethod(), User))
        {
            throw new NotAuthorizedException();
        }
    }
}

Ok. So what about managing the permissions? This is now the easiest part. What we have to do is to scan the assembly using reflection and search for methods that are marked with the MethodSecured attribute.

Then it is just a matter of assigning methods to a specific user and putting it into the database.

Some Notes About the Example Source Code

The solution contains three projects:

Conclusion

My solution isn't probably the best that exists, but for me it worked great since it can be totally independent from any build in security mechanisms and as a result, can be moved very easily.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralSource code missing
TBermudez
11:08 14 May '08  
Source code link is broken.
GeneralRe: Source code missing
Michal Kosmala
11:31 14 May '08  
Sorry,

editor has mixed something up.

It should be ok now.

Michal
GeneralFramework already has this
Nathan Evans
10:18 14 May '08  
What is wrong with:

void MyMethod() {
new PrincipalPermission(...).Demand();
}

???
GeneralRe: Framework already has this
Michal Kosmala
10:43 14 May '08  
There is noting wrong in using it.

But how can you than manage your access to your methods?? You have to know Exactly which methods are calling new PrincipalPermission(...).Demand();.

In my solution it is easy to get list of all secured methods by using reflection. Also if you use IsPermited without marking secured method with "MethodSecuredAttribute" there will be an exception thrown.


Last Updated 14 May 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010