java redirect 超时,OnActionExecuting造成无限重定向处理会话超时

I am trying to use OnActionExecuting in my BaseController to handle redirecting to LogOn screen if the User Session is timed out. However it is causing infinite redirects even before I get Logged in- Can anyone advise on how to get round this?

So in my Base controller I have the following:

protected override void OnActionExecuting(ActionExecutingContext filterContext)

{

if (SessionManager.Instance().GetUser() == null)

{

filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {

{ "Controller", "Home" },

{ "Action", "LogOn" }

});

}

base.OnActionExecuting(filterContext);

}

So basically I have a Session Manager class that can get/set a user etc - I kind of know the problem in that GetUser will also be null as it is only set using my SetUser method after LogOn has been validated.

But rather than having every method in my other controllers having a check like:

public ActionResult SomeOtherMethod()

{

if(SessionManager.Instance().GetUser() != null)

{

//Go Do Useful View stuff

}

else

{

//Redirect To Logon

}

}

I wanting to use OnActionExcuting in the Base Controller. But because it is running on the LogOn pages (as it should) I am getting infinte redierct because the GetUser will always be null?

Is there a better way off doing this

解决方案

Maybe you can try something like this :

protected override void OnActionExecuting(ActionExecutingContext filterContext)

{

string controllerName = filterContext.Controller.GetType().Name;

string actionName = filterContext.ActionDescriptor.ActionName;

if (SessionManager.Instance().GetUser() == null )

{

if(!controllerName.Equals(typeof(HomeController).Name,StringComparison.InvariantCultureIgnoreCase)

|| !actionName .Equals("LogOn",StringComparison.InvariantCultureIgnoreCase)))

filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {

{ "Controller", "Home" },

{ "Action", "LogOn" }

});

}

base.OnActionExecuting(filterContext);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值