I’m trying to design a web application that would user WCF services to access data and provide business logic. So in general the whole system would look like that:
UI (ASP.NET MVC)
BusinessLayer (WCF Services)
DataLayer (Entity Framework)
Date (SQL Server Database)
All parts of the system will resist on the same, closed environment, so I’m going to use Certificates to secure ASP.NET <-> WCF connection. Database connection would use standard EF securities, Connection String and Windows Authentication.
The application has to provide authentication and authorization functionality. I’m going to move most of that into ASP.NET, so there will be ValidateUserAuth() service method, which will be used to validate credentials, but the result (with UserRole that user belongs to) will be then used by ASP to create user session.
After that, every Service Method call needs to know the UserRole of current user, to return proper results (or say ‘Access denied’ if it’s necessary). Problem is I don’t want to pass that UserRole as a parameter for every Service Method! I’d like to make it happen automatically. Is it even possible with WCF?
All I need is:
- Every service call made from ASP.NET app will be extended with User data taken from current ASP Session.
- Service Method invoked by that call will be able to recieve that User data and use it to provide results according to user permissions.
- All this would happen somekind on background, so there will be no additional
UserDetailsmethod parameter added to every Service Method exposed from Service.
I read a lot about WCF itself, but found anything that could met my requirements. I hope I just missed it and it’s still possible.
I decided to use MessageInspector for that:
On Client-side:
And Server-side:
I also had to set custom AuthorizationPolicy to prevent standard one from overwriting Thread.CurrentPrincipal: