Session TimeOut is not working asp.net mvc

问题描述 投票:0回答:1

我想要的是,在应用程序中,如果用户超过2分钟未执行任何操作,我想将页面重定向到登录页面,表明会话已过期。因此,我尝试了类似以下的内容

在我的HomeController中

public class SessionTimeoutAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {            
        HttpContext ctx = HttpContext.Current;

        var strSession = HttpContext.Current.Session;
        if (strSession == null)
        {
            filterContext.Result = new RedirectResult("Login");                
        }
        base.OnActionExecuting(filterContext);
    }
}

以及在我添加的每个控制器方法中都这样

[SessionTimeout]
public class AppController : Controller
{}

下面也这样

[HttpGet]
    public ActionResult Assign()
    {
        string validUser = "";
        string action = "";
        string controller = "";
        List<UMSLocationDetails> lstUMSLocationDetails = new List<UMSLocationDetails>();
        HomeController homeController = new HomeController();
        string assignUser = homeController.CheckRole(Convert.ToString(TempData["LoginUserName"]), Convert.ToString(TempData["strCurrentGroupName"]));

        if (Convert.ToString(TempData["strCurrentGroupName"]) != assignUser || Convert.ToString(TempData["strCurrentGroupName"]) == "" || Convert.ToString(TempData["strCurrentGroupName"]) == null)
        {
            return RedirectToAction("Login", "Home");
        }
        else
        {
            if (TempData["Location"] != null)
            {
                lstUMSLocationDetails = TempData["Location"] as List<UMSLocationDetails>;
                ViewBag.LocationDetails = lstUMSLocationDetails;
                TempData.Keep();
                //TempData.Remove("Location");
                ViewBag.LoginUserName = Convert.ToString(TempData["LoginUserName"]);
                //ViewBag.LoginUserName = Convert.ToString(Session["LoginUserName"]);  
                ViewBag.CurrentGroupName = Convert.ToString(TempData["strCurrentGroupName"]).Replace("_", " ");
                ViewBag.strReturnMessage = Convert.ToString(TempData["strReturnMessage"]);
                TempData.Remove("strReturnMessage");
                if (assignUser == strSapUserRole)
                {
                    validUser = "";
                    action = "Assign"; controller = "App";
                }
                else
                {
                    validUser = "1";
                    // return RedirectToAction("Login", "Home");
                    action = "Login"; controller = "Home";

                }
                //TempData.Remove("LoginUserName");
                //TempData.Remove("strCurrentGroupName");
            }
            if (validUser == "1")
            {
                return RedirectToAction("Login", "Home");
            }
            else
            {
                return View();
            }
        }

    }
    [HttpGet]
    public ActionResult Certify()
    {
        string validUser = "";
        string action = "";
        string controller = "";
        List<UMSLocationDetails> lstUMSLocationDetails = new List<UMSLocationDetails>();
        HomeController homeController = new HomeController();
        string certifyUser = homeController.CheckRole(Convert.ToString(TempData["LoginUserName"]), Convert.ToString(TempData["strCurrentGroupName"]));
        //  string certifyUser = "NEIQC_FIBER_ENGINEER";
        if (Convert.ToString(TempData["strCurrentGroupName"]) != certifyUser || Convert.ToString(TempData["strCurrentGroupName"]) == "" || Convert.ToString(TempData["strCurrentGroupName"]) == null)
        {
            return RedirectToAction("Login", "Home");
        }
        else
        {
            if (TempData["Location"] != null)
            {
                lstUMSLocationDetails = TempData["Location"] as List<UMSLocationDetails>;
                ViewBag.LocationDetails = lstUMSLocationDetails;
                TempData.Keep();
                //TempData.Remove("Location");
                ViewBag.LoginUserName = Convert.ToString(TempData["LoginUserName"]);
                ViewBag.CurrentGroupName = Convert.ToString(TempData["strCurrentGroupName"]).Replace("_", " ");
                TempData.Keep();
                if (certifyUser == strFEUserRole)
                {
                    validUser = "";
                    action = "Certify"; controller = "App";
                }
                else
                {
                    validUser = "1";
                    // return RedirectToAction("Login", "Home");
                    action = "Login"; controller = "Home";
                }
            }
            if (validUser == "1")
            {
                return RedirectToAction("Login", "Home");
            }
            else
            {
                return View();
            }
            // return View();
            // return RedirectToAction(action, controller);
        }
    }
    [HttpGet]
    public ActionResult Approver()
    {
        string validUser = "";
        string action = "";
        string controller = "";
        List<UMSLocationDetails> lstUMSLocationDetails = new List<UMSLocationDetails>();
        HomeController homeController = new HomeController();
        string aprroverUser = homeController.CheckRole(Convert.ToString(TempData["LoginUserName"]), Convert.ToString(TempData["strCurrentGroupName"]));
        if (Convert.ToString(TempData["strCurrentGroupName"]) != aprroverUser || Convert.ToString(TempData["strCurrentGroupName"]) == "" || Convert.ToString(TempData["strCurrentGroupName"]) == null)
        {
            return RedirectToAction("Login", "Home");
        }
        else
        {


            if (TempData["Location"] != null)
            {
                lstUMSLocationDetails = TempData["Location"] as List<UMSLocationDetails>;
                ViewBag.LocationDetails = lstUMSLocationDetails;
                TempData.Keep();
                //TempData.Remove("Location");
                ViewBag.LoginUserName = Convert.ToString(TempData["LoginUserName"]);
                ViewBag.CurrentGroupName = Convert.ToString(TempData["strCurrentGroupName"]).Replace("_", " ");
                if (aprroverUser == strCMMpUserRole)
                {
                    validUser = "";
                    action = "Certify"; controller = "App";
                }
                else
                {
                    validUser = "1";
                    // return RedirectToAction("Login", "Home");
                    action = "Login"; controller = "Home";

                }
            }
            if (validUser == "1")
            {
                return RedirectToAction("Login", "Home");
            }
            else
            {
                return View();
            }
            // return View();
            // return RedirectToAction(action, controller);
        }
    }

我尝试了上面的代码,但没有任何反应。请提出实现此目标的最佳方法。

c# asp.net-mvc session-timeout
1个回答
0
投票

如果您要检查用户会话,为什么不使用Authorized过滤器呢?

© www.soinside.com 2019 - 2024. All rights reserved.