I’m developing some website which is a kind of online workplace, there will be some users and some ongoing computer programming projects, and each user can have multiple roles, for example one particular user can be a project manager for an project and a developer for another project. naturally the project manager has more authority than the developer in the project. my question is how to manage this in my code neatly? I was going to use my custom Role Provider and use the Authorize attribute with this, but it’s not sufficient , since I’d need the project Id plus the user Id to find the role of user in an specific project.
Share
First all you will have to create additional tables for your extended role management like
projectsand there relationship with theusersin context ofoperations, which might be yourcontroller's actions.One way of doing is to create your own table for
roles. In that case you will only use only Asp netmembership users, but it all depends your requirements.Secondly you have to handle it in
MVC, In my opinion the best way is to implement it through your own customAuthorizationattribute, and decorate your controller’s actions with your custom authorization attribute instead of[Authorization]attribute.Its very simple.
For that you have to inherent your class from
FilterAttributeand also have to implementIAuthorizationFilterinterface.In the method
OnAuthorization, you can get all the information which you might be require in your custom authorization logic likeHttpContext,Controllername,Actionname. You have to just call your custom authentication logic from this method.Your custom authentication logic might look like the following.