从AuthorizeCore重定向到另一个具有自定义错误消息的控制器

问题描述:

所以我试图向用户显示一条消息,当他从他未被授权查看的页面重定向时。例如:用户转到www.mypage.com/users并重定向到www.mypage.com/home。从AuthorizeCore重定向到另一个具有自定义错误消息的控制器

我使用MVC模式,它是asp.net web应用程序。 我重写AuthorizeAttribute和AuthorizeCore方法尝试这样的:

protected override bool AuthorizeCore(HttpContextBase httpContext) 
     { 


      if (false)//Here is custom logic that is working 
      { 
       string message = "You don't have an access to selected menu item."; 
       var dataDict = HttpContext.Current.Session["__ControllerTempData"] as IDictionary<string, object>; 
       if (dataDict == null) 
       { 
        Dictionary<string, object> dictionary = new Dictionary<string, object>(); 
        dictionary["myErrorMessage"] = message; 
        HttpContext.Current.Session["__ControllerTempData"] = dictionary; 
       } 
       else 
       { 
        dataDict["myErrorMessage"] = message; 
        HttpContext.Current.Session["__ControllerTempData"] = dataDict; 
       } 

       _isAuthorized = false; 

       httpContext.Response.Redirect("Home");     
      } 
      else 
      { 
       _isAuthorized = base.AuthorizeCore(httpContext); 
      } 

      return _isAuthorized; 
     } 

然后我试图从该视图访问它

var unauthorized_access_message = TempData["myErrorMessage"] as List <string> ?? new List<string>() ; 

但它不工作。我也试过this,但不是这样,因为我试图访问一个控制器,然后重定向到另一个。 是否有任何解决方案将变量传递给视图或检查某些状态(如重定向原因)?

我通过使用Cookie来解决它。

在CustomAuthorizeAttribute设置的cookie:AuthorizeAttribute类:

protected override bool AuthorizeCore(HttpContextBase httpContext) 
      { 

       if (false) 
       { 
        HttpCookie mycookie= new HttpCookie("unauthorize_error", "You do not have access to requested link."); 
        httpContext.Response.SetCookie(mycookie); 
        httpContext.Response.Redirect("Home"); 
        return false; 
       } 
       else 
       { 
        return base.AuthorizeCore(httpContext); 
       } 
      } 

然后在控制器:

string cookieValue = string.Empty; 
      if (Request.Cookies["myCookieName"] != null 
       && !string.IsNullOrEmpty(Request.Cookies["myCookieName"].Value)) 
      { 
       cookieValue = Request.Cookies["myCookieName"].Value; 
       var myCookie = new System.Web.HttpCookie("myCookieName"); 
       myCookie.Expires = System.DateTime.Now.AddDays(-1d); 
       Response.Cookies.Add("myCookieName"); 
      } 
var messages = TempData["mytemperror"] as List<string> ?? new List<string>(); 
      messages.Add(message); 
      TempData["myTempError"] = messages; 
鉴于

最后:

var errorMessage = TempData["myTempError"] as List<string> ?? new List<string>(); 

而不是AuthorizeCore,你可以覆盖OnAuthorization其中您可以直接从Controller访问TempData

public override void OnAuthorization(AuthorizationContext filterContext) 
{ 
    if (false) 
    { 
     filterContext.Controller.TempData["myErrorMessage"] = 
      "You don't have an access to selected menu item."; 
    } 
    base.OnAuthorization(filterContext); 
} 

使用

string unauthorized_access_message = (TempData["myErrorMessage"] ?? "").ToString(); 
+0

很抱歉,但不起作用。我认为这是因为重定向。比方说,有人试图访问帐户控制器,但没有权限(它会打OnAuthorization并设置tempdata),所以他将被重定向到主控制器(它会再次打到OnAuthorization,但之后就没有那个TempData了)。至少当我尝试从Home/Index访问它时,它是空字符串。 – maximelian1986

+0

通常情况下,匿名用户应该被重定向到“登录页面”而不是“主页”,除非主页都是匿名访问。 – Win

+0

问题是它不是匿名用户。我有自定义身份验证。因此,用户通过身份验证但未获得授权,可以说管理员可以访问帐户/成员,他可以在其中获得成员名单。但是,如果会员尝试访问它,他必须转到主页/索引并看到“没有权限”通知。 – maximelian1986