|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis article describes an easy, manageable and light way to secure methods in your application. BackgroundLately, 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 SolutionsAfter some searching, I have found two solutions that have partially been solving my problem. .NET Framework Built In Security.NET Framework offers the 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:
Imperative CallThis 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();
}
}
}
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 SolutionThe 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 So now 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 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 CodeThe solution contains three projects:
ConclusionMy 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
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||