we have developed an intranet application with Silverlight 4 using MVVM with PRISM. Up until now we only had a basic security scheme which boils down to “access granted” or “access denied” based on active directory group membership. Now we have to expand on that.
We defined more granular roles & permissions which are being loaded at app startup and which are being exposed by a singleton ISecurityContext instance. This security context knows about the roles & permissions granted to the currently logged on user. Now I’d like to plug this context into my view models in an elegant way. A naive example of what I’d like to do is this:
public class NavigationBarViewModel
{
//...
[Secured(RequiredPermission="EditLocation")]
public void NavigateToEditLocations(IRegionManager rManager)
{
var editLocView = new Uri("EditLocationsView", UriKind.Relative);
rManager.RequestNavigate("WorkspaceRegion", editLocView);
}
//...
}
Now of course the Secured Attribute should somehow be aware of our security context. I’m not sure where to start or if this would really be an application of the decorator pattern.
Mabye someone can point me into the right direction.
So after some research I think I found a clean way for cross cutting security using Unity interception. I’ll share my code:
As you can see it’s all about creating a custom HandlerAttribute which in turn calls a custom ICallHandler. In the CallHandler you can put your security related logic which will decide if the decorated method will be executed. You should throw some custom exceptions that you can catch from the layer using your methods. With that infrastructure in place, you can integrate any cross cutting modules in a clean way. Here’s the config for the application since this can be a bit of a pain in Unity. Dont forget to add the Microsoft.Practices.Unity.InterceptionExtension namespace in your classes btw.