【发布时间】:2019-02-28 07:59:14
【问题描述】:
所以我有两个自定义授权属性:1)是在会话过期或未通过身份验证时重定向用户登录; 2) 目前正在进行中。
第二个自定义授权属性的想法是在用户导航到下一页之前将用户重定向到同一页面或防止重定向到下一页请求。假设代码是
public class CustomAuth2Attribute : AuthorizeAttribute
{
private const string _errorController = "Error";
public override void OnAuthorization(AuthorizationContext filterContext)
{
var controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
var action = filterContext.ActionDescriptor.ActionName;
var area = "";
if (filterContext.RouteData.DataTokens.ContainsKey("area"))
area = filterContext.RouteData.DataTokens["area"].ToString();
if (controller == _errorController)
{
return;
}
// checking the user identity whether the user is allowed to access this page
// then redirect to the previous page before this request and add flash note: "not allowed to access the content"
}
}
我们的想法是,如果用户无权访问某个页面,我不会将此标记为未授权,而是应该将他们返回到他们之前的页面,并附上注释消息。
还尝试了以下代码:
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
{
controller,
action,
area
}));
我收到了太多重定向,这是因为我引用的是当前控制器、动作和区域,而不是前一个。我也尝试获取 UrlReferrer 值,但这始终是null。
我有什么办法可以做到这一点?任何帮助表示赞赏。提前谢谢你。
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-4 authorize-attribute