For some time around this problem, I came to the following conclusion.
Security implementation like Authentication , Role-based security , Authorization , etc. At the user experience level, it is not a good idea mainly for two reasons:
- If you want to create other user interfaces for your application, say,
WinForms or Silverlight , you will have to implement this protection again from scratch. - You can always use other components / levels of your system without creating a user interface. Suppose you are creating a simple console application that references other levels on your system (i.e.
Repositories ). then you can instantiate the repository and manage the data. in this case, you have successfully redefined any security.
Thus, the solution is to implement this in-house security at a different level, built into the domain model itself and not tied to a user interface (UI).
Now there are several options for how you can do this. let's say we have a layer called AppService , which is the entry point to the whole system. this layer consists of messages (a messaging template, for example, a Request-Response template), ViewModels , which are flattened views of domain objects and Methods for retrieving and manipulating data , etc., here we can implement such security measures using PrincipalPermission objects. we can apply security rules to classes and methods. here is a simple example:
[PrincipalPermission(SecurityAction.Demand, Authenticated=true)] public void DoSomething() { }
With the attribute defined for this method, the code requires that the caller be authenticated. authentication model can be any, including Windows Authentication , Forms Authentication and so on. Now this works fine, because now we have loosely coupled the user interface with the security rules defined at the service level. however, there is still a catch.
This design will work fine, since the service level is indeed the main entry point for the system. that if you can still instantiate, say, in the repository, without having to get an instance of your AppService, you can still override the security rules. that, as they say, you should design your domain model in such a way that an instance of AppService is required to work with the components / levels of your system. in this case, any function provided in the system is available only through the application service level. on the other hand, if this is not possible or does not bother at the moment, you will also have to define your security rules in other layers. which means that you also have to define security rules in your repositories. so if someone creates an instance of the repository and tries to manipulate the data without executing his commands through the UI or AppService , you still apply security measures.
Another thing is that using PrincipalPermission rules for your classes and methods is not tied to a specific authentication / authorization model. therefore, you can use such security rules in web applications using Forms Authentication or Windows applications using Windows/AcctiveDirectory Authentication , etc.
As you remember, I am developing a simple blogging engine in ASP.NET , and this model is working fine at the moment. if there are more detailed in-depth pros and cons, or samples and blog posts that may help in this section, be sure to post your comments and answers [: