Omar
Omar

Reputation: 40182

ASP.NET MVC Check if user can perform action

I have a collection of posts created by separate users in my database.

If the user did indeed make the post, it renders the view. In the view there is a hidden field with the "PostID".

When the user submits the form, the POST part of the action rechecks if the PostID matches a post created by the current logged in User.

Is there a better way to accomplish this without having to double check if the user has access to edit the post, or is this the best way?

Upvotes: 0

Views: 703

Answers (2)

griegs
griegs

Reputation: 22760

I had the same problem but I solved it by removing the ability to edit at the View. Once the ability to edit was removed I could kinda assume that if an edit was made then the user had rights.

I did this because others, moderators, also had to be able to edit the posts.

Clearly this might not work on the internet because you could get around the security but on the company intranet it works a treat.

I still think I should check at the business layer for the user rights but they need to give me more money and time for that. :)

Upvotes: 0

Blair Scott
Blair Scott

Reputation: 1885

That actually sounds pretty good to me. The only thing I could suggest if you didn't want to check the user twice would be to use something like an anti-forgery token that you render with the view. Since you only get to the view on if you are allowed to edit the post, at this point I would think all you need to do is check to see if the POST came from your site.

Upvotes: 1

Related Questions