umki
umki

Reputation: 779

Extending Asp.Net Identity IsInRole() Method

In our project management application, we need to keep access rights (per project for each user) in a separate table in db. IE.

User A can create tasks in Project #1, User A can only read task in Project #2

To succeed that, we extend our AspNetUserRoles table by adding a new coloumn(projectID).

By default, in razor view, we can check user roles with User.IsInRole("CanCreateTask"). We need to extend this method and want to check the user role for specific project Ie. User.IsInRole("CanCreateTask", project.Id)

EDITED: Also i need to check user access in controller with project.Id

[Authorize(Roles = "CanCreateTask")]

Did some research on net but could not find any solution.

Upvotes: 1

Views: 2295

Answers (1)

Matt Hensley
Matt Hensley

Reputation: 883

You could create an extension method, extending the User class.

public static bool IsInRole(this User user, string role, int projectID)
{
    var isInRole = user.IsInRole(role);
    var hasRoleInProject = // Logic for deciding if it is in the role for this project

    return isInRole && hasRoleInProject; 

}

This method would be called like this:

user.IsInRole("CanCreateTask", project.Id);

Upvotes: 4

Related Questions