在我们的项目管理应用程序中,我们需要将访问权限(每个用户的每个项目)保存在 db 的单独表中。 IE。
用户 A 可以在项目 #1 中创建任务, 用户 A 只能读取项目 #2 中的任务
为了成功,我们通过添加一个新的列(projectID)来扩展我们的 AspNetUserRoles 表。
默认情况下,在 razor View 中,我们可以使用 User.IsInRole("CanCreateTask")
检查用户角色。我们需要扩展此方法并希望检查特定项目 Ie 的用户角色。 User.IsInRole("CanCreateTask", project.Id)
已编辑:我还需要使用 project.Id 检查 Controller 中的用户访问权限
[Authorize(Roles = "CanCreateTask")]
在网上做了一些研究,但找不到任何解决方案。
最佳答案
你可以创建一个 extension method , 扩展 User 类。
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;
}
这个方法会这样调用:
user.IsInRole("CanCreateTask", project.Id);
关于c# - 扩展 Asp.Net Identity IsInRole() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35021396/