使用 actionfilterattribute 基于 mvc 角色的权限

发布于 2024-11-28 07:30:24 字数 1542 浏览 3 评论 0原文

所以我正在为 mvc 网站设置权限。我正在执行基于角色的权限,在控制器中执行操作将需要不同的角色,具体取决于操作的目的。 我知道最推荐的是authorizeattribute(因为我希望缓存角色),但是是否可以使用actionfilterattribute 进行相同的操作?

目前我有一个与此类似的 actionfilterattribute:

public class PermissionRequired : ActionFilterAttribute{
   private readonly Role reqrole;
   public PermissionRequired(Role reqRole)
   {
         reqrole = reqRole;
   }

   public override void OnActionExecuting(ActionExecutingContext filterContext) {
        var ctrl = (GeneralController)filterContext.Controller;

        if (!ctrl.CurrentUser.InRole(reqrole)) {
               //some code to redirect this to a certain page
        }
        base.OnActionExecuting(filterContext);
    }
}

在 GeneralController 上获取当前用户

public class GeneralController : Controller

    private User currentUser;
    public User CurrentUser {
        get {
            if (currentUser != null)
                return currentUser;

            int currentUserId = Convert.ToInt32(httpContext.User.identity.Name); 

            if (currentUserId != 0) {
                this.currentUser = Tds.Users.FirstOrDefault(u => u.Id == currentUserId)
            }

            return currentUser;
        }
    }

,在将继承此属性的控制器上,

[PermissionRequired(Role.Moderator)]
public class SomeControllerThatNeedsPermission
{
    [PermissionRequired(Role.SuperAdmin)]
    public ActionResult SomeActionThatNeedsPermission()
      {
      }
}

因此,任何人的帮助都会受到赞赏..甚至欢迎评论或想法:D

非常感谢!

So I'm setting up my permission for an mvc website. And I'm doing a role based permission, having actions in a controller would require different Roles depending on the purpose of the action.
I know that the most recommended would be authorizeattribute (as i want the roles cached) but is it possible to have the same with the actionfilterattribute?

Currently I have an actionfilterattribute similar to this:

public class PermissionRequired : ActionFilterAttribute{
   private readonly Role reqrole;
   public PermissionRequired(Role reqRole)
   {
         reqrole = reqRole;
   }

   public override void OnActionExecuting(ActionExecutingContext filterContext) {
        var ctrl = (GeneralController)filterContext.Controller;

        if (!ctrl.CurrentUser.InRole(reqrole)) {
               //some code to redirect this to a certain page
        }
        base.OnActionExecuting(filterContext);
    }
}

and on the GeneralController to get the current User

public class GeneralController : Controller

    private User currentUser;
    public User CurrentUser {
        get {
            if (currentUser != null)
                return currentUser;

            int currentUserId = Convert.ToInt32(httpContext.User.identity.Name); 

            if (currentUserId != 0) {
                this.currentUser = Tds.Users.FirstOrDefault(u => u.Id == currentUserId)
            }

            return currentUser;
        }
    }

and on the controllers that will inherit this attribute

[PermissionRequired(Role.Moderator)]
public class SomeControllerThatNeedsPermission
{
    [PermissionRequired(Role.SuperAdmin)]
    public ActionResult SomeActionThatNeedsPermission()
      {
      }
}

so, anybody help is appreciated.. even comments or thoughts are welcome :D

Thanks much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够爱げ你 2024-12-05 07:30:24

您似乎没有在这里使用自定义会员资格。在这种情况下,使用 actionfilterattribute 执行此操作有些毫无意义,但仍然可行。

这是一篇关于同一主题的优秀文章 - 扩展 AuthorizeAttribute 来执行基于角色的验证并返回自定义错误...

只有当您希望向用户显示授权失败时发生的情况(未显示 401,它会变成 302)时,这样做的价值才会出现(如文章中所述)内部在MVC 管道)

It seems like you are not using custom membership here. In which case doing this with a actionfilterattribute is somewhat pointless, but nonetheless do able.

This is an excellent article on the same subject - extending the AuthorizeAttribute to perform role based validation and return custom errors...

The value in doing that also only comes across (as explained in the article) when you wish to show users whats going on when the Authorization fails (the 401 is not shown it turns into a 302 internally in the mvc plumbing)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文