Reputation: 311
I'm having a problem of designing a common functionality of hiding information created by a user from other users. As an example,to edit Products created by USER1, normally we use one of following.
My concern is, if USER2 got the Id, 1, he also able to access Product with Id=1, which was created by USER1. How to restrict USER2 accessing USER1'S data? This may needs to apply for every module in the project. Is there a common way to achieve this? Thanks
Upvotes: 0
Views: 1081
Reputation: 679
If you are keeping state of what user is accessing the data. You can add a "WHERE CreatedBy = {YOURLOGGEDINUSER}"
to this query and throughout your application. Then even if he gets the ID correct no data would be returned.
Upvotes: 2
Reputation: 7434
Assuming that you have enabled some sort of ASP.Net Authentication (The user has proved who they are) then you now need to think about the Authorization (what the user is allowed to do).
It doesn't help that these two terms are often combined or used interchangeably. In MVC a custom AuthorizeAttribute
is often used to do both.
For managing records, the current logged on user is accessed via the IPrincipal
from HttpContext.Current.User
.
The user id is usually set at HttpContext.User.Current.Identity.Name
although you may need to do a null check if not every route is authenticated.
Upvotes: 1