OnActionExecuting造成无限重定向处理会话超时 [英] OnActionExecuting Causing Infinite Redirect for handling Session Timeout
问题描述
我想使用OnActionExecuting我BaseController处理重定向如果用户会话超时登录屏幕。但是它是导致无限重定向我之前也被记录到日志IN-任何人都可以就如何克服这个建议?
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);
}
所以基本上我有一个可以获取/设置用户等一个Session Manager类 - 我有点知道在的getUser问题也将是空的,因为它只是用我的SETUSER法登录已验证后置
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
}
}
我想在基地控制器使用OnActionExcuting。但由于它是在登录页上运行(因为它应该)我越来越infinte redierct因为的getUser将永远是空?
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);
}
这篇关于OnActionExecuting造成无限重定向处理会话超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!