【问题标题】:Authorize one action in a controller but don't require a role授权控制器中的一项操作,但不需要角色
【发布时间】:2013-07-08 13:09:12
【问题描述】:

我有一个产品控制器,需要“product_editor”角色来访问大多数方法。有一些操作不需要授权,因此 [AllowAnonymous] 效果很好。但是,我有一项操作需要他们登录,但他们可能不是产品编辑器。

有没有一种简单的方法来为一个动作声明这个?

你可以看到我的一些尝试被注释掉了

[Authorize(Roles = "product_editor")]
public class ProductController : Controller
{
    #region Public Actions
    [AllowAnonymous]
    public ActionResult Search(string keyword...

    [AllowAnonymous]
    public ActionResult Details(string id...
    #endregion

    //[Authorize]
    //[Authorize(Roles="")]
    public ActionResult AuthorizedDownload(long id, string step)
    {
        SecureDownloadLink link = SecureDownloadLink.Load(id);
        if(link.belongsTo(HttpContext.Current.User.Identity.Name))
        {
            //Allow download
        }
        else
        {
            //Return 404 Error
        }
    }
}

--编辑--

找到了一个可行的解决方案,但我喜欢基于属性的解决方案,因为我的其余身份验证是在属性中完成的,而且 [AllowAnonymous] 有点误导。

[AllowAnonymous]
public ActionResult AuthorizedDownload(long id, string step)
{
    if (!User.Identity.IsAuthenticated)
        return RedirectToAction("Login", "Account", new { ReturnUrl = Request.Url.LocalPath });
....
}

【问题讨论】:

    标签: asp.net-mvc roles authorize


    【解决方案1】:

    除了在每个控制器操作上明确指定 Authorize 属性外,我认为没有简单的方法可以实现:

    public class ProductController : Controller
    {
        #region Public Actions
        [AllowAnonymous]
        public ActionResult Search(string keyword...
    
        [AllowAnonymous]
        public ActionResult Details(string id...
        #endregion
    
        [Authorize]
        public ActionResult AuthorizedDownload(long id, string step)
        {
            SecureDownloadLink link = SecureDownloadLink.Load(id);
            if(link.belongsTo(HttpContext.Current.User.Identity.Name))
            {
                //Allow download
            }
            else
            {
                //Return 404 Error
            }
        }
    
        [Authorize(Roles = "product_editor")]
        public ActionResult SomeOtherAction()
        {
            ...
        }
    }
    

    或者如果你有很多动作,另一种可能性是在单独的控制器中移动不同的动作。

    【讨论】:

      【解决方案2】:

      授权使用级联规则,因此您可以通过这种方式简化它。

      [Authorize]
      public class ProductController : Controller
      {
          #region Public Actions
          [AllowAnonymous]
          public ActionResult Search(string keyword...
      
          [AllowAnonymous]
          public ActionResult Details(string id...
          #endregion
      
          public ActionResult AuthorizedDownload(long id, string step)
          {
              SecureDownloadLink link = SecureDownloadLink.Load(id);
              if(link.belongsTo(HttpContext.Current.User.Identity.Name))
              {
                  //Allow download
              }
              else
              {
                  //Return 404 Error
              }
          }
      
          [Authorize(Roles = "product_editor")]
          public ActionResult SomeOtherAction()
          {
              ...
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-13
        • 1970-01-01
        • 1970-01-01
        • 2011-07-02
        • 1970-01-01
        • 1970-01-01
        • 2011-02-13
        • 2020-02-21
        相关资源
        最近更新 更多