【发布时间】:2018-12-02 07:47:52
【问题描述】:
我有一个自定义的授权属性:
using System;
using System.Web.Mvc;
using System.Web.Routing;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (!filterContext.HttpContext.Request.IsAuthenticated)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Login", action = "Login" }));
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}
}
}
...我用来装饰某些控制器的:
[MyAuthorizeAttribute(Roles = "Superman, Batman, Spiderman")]
public class SuperHeroController : Controller
{
// ....
}
谁能解释一下如何修改授权代码,以便如果授权失败,登录 URL 包含ReturnUrl(当前控制器/方法的 URL)?
这基本上是在尝试模仿 Web 表单 ReturnUrl 逻辑,但采用一种智能方式,因此我不必手动为 URL 使用字符串。
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-mvc-5.2