【问题标题】:Display action-specific authorisation message for [Authorize] attribute显示 [Authorize] 属性的特定于操作的授权消息
【发布时间】:2010-04-13 15:49:04
【问题描述】:

当[Authorize] 或[Authorize(Roles="Administrator")] 属性将用户重定向到登录页面时,有没有办法显示特定于操作的授权消息?

理想情况下,

[Authorize(Roles="Administrator", Message="I'm sorry Dave. I'm afraid I can't let you do that.")]
public ActionResult SomeAdminFunction()
{
    // do admin stuff
    return View();
}

据我了解,属性并不意味着添加功能,但这似乎纯粹是信息性的。可以在动作中执行此操作,但与使用属性相比,这似乎不够优雅。

或者,

if (!Request.IsAuthenticated)
{
    if (!User.IsInRole("Administrator"))
        SetMessage("You need to be an administrator to destroy worlds."); // write message to session stack
    return RedirectToAction("SignIn", "Account");
}

是否有现有的方法可以做到这一点,还是我需要覆盖 [Authorize] 属性?

【问题讨论】:

标签: asp.net-mvc authorization authorize-attribute


【解决方案1】:

我会覆盖该属性以添加我的特定消息。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 2019-08-12
  • 2014-04-01
相关资源
最近更新 更多