Alex Rivera | Logout

asp.net mvc authorization using roles

Asked 2008-12-24T06:54:38.843
24

I'm creating an asp.net mvc application that has the concept of users. Each user is able to edit their own profile. For instance:

Nothing particularly exciting there...

However, I have run into a bit of trouble with the Authorization scheme. There are only two roles in the system right now, "Administrator" and "DefaultUser", but there will likely be more in the future.

I can't use the regular Authorize attribute to specify Authorization because both users are in the same role (i.e., "DefaultUser").

So, if I specify the Authorize Filter like so:

[Authorize(Roles = "DefaultUser")]

then there is no effect. PersonID=1 can go in and edit their own profile (as they should be able to), but they can also just change the URL to http://localhost/person/edit/2 and they have full access to edit PersonID=2's profile as well (which they should not be able to do).

Does this mean that I have to create my own Authorization filter that checks if the action the user is requesting "belongs" to them before allowing them access? That is, if the edit action, with parameter = 1 is being requested by the currently logged in person, do I need to do a custom check to make sure that the currently logged in person is PersonID=1, and if so, authorize them, and if not, deny access?

Feels like I'm missing something obvious here, so any guidance would be appreciated.

Edit
Report

1 Answer

10

Matt is right.

What the authorisation is for is to show that they're allowed to perform that function - what you're trying to do is say whether they can perform the function for that particular ID.

So two solutions:

  1. Like Matt said, make an action that takes no ID, but looks up the current logged in user from the session information, and retrieves them.
  2. Make an action that takes an ID, but only allow administrators access - so they can modify other users information if required.

But to answer the question, the Authorisation is only to say "Yes, this person can use the modify user action", not based on the parameter entered.

The other way is that you could make it check that the user retrieved == the current user, or redirect to another action saying that they cannot edit that user - but it'd be better just to provide an action that doesn't take an id, and just gets the current logged in user.

answered 2008-12-24T07:25:29.797

Your Answer