2

My google skills are failing me on this. I'm looking for the "right way" to do data based entitlements in ASP.Net MVC (3).

With regular entitlements where one just need to know the user and the route can be done with the [Authorize] attribute, but this doesn't appear to work with data based entitlements b/c of the need to have a connection to the data store.

Is the obvious approach of inserting a check into the action methods the right way?

3
  • What is a "data based entitlement?" Do you mean "security" or "authorization?" Try looking for "table-based security in ASP.NET MVC". Commented May 23, 2011 at 0:15
  • @Robert -exactly, I've updated the title to reflect this Commented May 23, 2011 at 0:25
  • stackoverflow.com/questions/1482135/custom-authorizeattribute Commented May 23, 2011 at 0:27

3 Answers 3

1

Is the obvious approach of inserting a check into the action methods the right way?

That's what I do.

if (!userHasAuthorization)
   return view("Unauthorized");

It's by far the simplest way.

To make sure you only have to do "userHasAuthorization" once, you can put a method in your repository or service layer that checks for authorization, and use that in place of the boolean value userHasAuthorization.

Sign up to request clarification or add additional context in comments.

Comments

0

Without knowing what "data based entitlements" are. I do believe that custom action filters will get you what you want. This lets you manage whatever you need around authorization with having the context of the route, user, etc. Gives more fine grained control. Also gives you the re usability so you dont need to plug if statements into your action methods.

http://msdn.microsoft.com/en-us/library/dd381609.aspx

2 Comments

I think what he's asking is: How do you look up authorization values from the database without tightly coupling your custom AuthorizeAttribute to the database? It's something I've often wondered myself. The custom AuthorizeAttribute does not have access to your repository, but by the time you've already entered the Action Method, it is too late.
Then I would go with this approach I mentioned here, the custom action filter lets you return before entering the action method.
0

You could create a custom action filter derived from the [Authorize] attribute that uses the data store to check authorization.

1 Comment

Any specifics on how to do that - meaning, how does one give the MyAuthorize class an instance of IDBConnection ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.