【问题标题】:How can I redict to same page after session expire in MVC project?如何在 MVC 项目中的会话过期后重定向到同一页面?
【发布时间】:2018-03-12 05:50:04
【问题描述】:
    //I Have a Action Method 
    [HttpGet]
    public ActionResult Login()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Login(VmUser_User VmUser_User)
    {
        if (VmUser_User.User_User.UserName == null || 
        VmUser_User.User_User.Password == null)
        {
            VmUser_User.LblError = "Please enter Username and Password";
            return View(VmUser_User);
        }
        //Return valid user
        if (VmUser_User.LoginUser() > 0)
        {

            Session["One"] = VmUser_User;
            return RedirectToAction("Index", "Home");

        }
        else
        {
            VmUser_User.LblError = "User/Password does not match!";
        }

        return View(VmUser_User);
    }

   //And another Action Method
    public async Task<ActionResult> Common_Unit()
    {
        Oss.Romo.ViewModels.User.VmUser_User user = 
        (Oss.Romo.ViewModels.User.VmUser_User)Session["One"];
        if (user == null)
        {
            return RedirectToAction("Login", "Home");
        }
        vmCommon_Unit = new VmCommon_Unit();
        await Task.Run(() => vmCommon_Unit.InitialDataLoad());

        return View(vmCommon_Unit);
    } 

当一个有效的用户登录应用程序时,它会重定向到 Home/Index 页面,然后他请求 Common/Common_Unit 页面。会话到期后,用户重新登录我想在最后请求的页面(如 Common/Common_Unit)中重定向的应用程序,请有人帮我解决这个问题。

我的问题:当授权用户浏览特定页面时,他会在一段时间内处于非活动状态。在最短时间会话结束,用户进入登录页面。登录后,我想在此特定页面上重定向用户。对不起我的英语不好

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    尝试使用ReturnUrl参数,像这样:

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(VmUser_User VmUser_User)
        {
            if (VmUser_User.User_User.UserName == null ||
            VmUser_User.User_User.Password == null)
            {
                VmUser_User.LblError = "Please enter Username and Password";
                return View(VmUser_User);
            }
            //Return valid user
            if (VmUser_User.LoginUser() > 0)
            {
    
                Session["One"] = VmUser_User;
    
                if (Request.QueryString["ReturnUrl"] != null & Request.QueryString["ReturnUrl"] != "")
                {
                    Response.Redirect(Request.QueryString["ReturnUrl"]);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
    
            }
            else
            {
                VmUser_User.LblError = "User/Password does not match!";
            }
    
            return View(VmUser_User);
        }
    
        //And another Action Method
        public async Task<ActionResult> Common_Unit()
        {
            Oss.Romo.ViewModels.User.VmUser_User user =
            (Oss.Romo.ViewModels.User.VmUser_User)Session["One"];
            if (user == null)
            {
                return RedirectToAction("Login", "Home", new { ReturnUrl = "/Common/Common_Unit" });
            }
            vmCommon_Unit = new VmCommon_Unit();
            await Task.Run(() => vmCommon_Unit.InitialDataLoad());
    
            return View(vmCommon_Unit);
        } 
    

    【讨论】:

    • 感谢您的回答,但不适合我。我的问题:当授权用户浏览特定页面时,他会在一段时间内处于非活动状态。在最短时间会话结束,用户进入登录页面。登录后,我想在此特定页面上重定向用户。对不起我的英语不好。
    • 抱歉没能帮到你:(希望能帮到你
    • 欢迎 Eva Lai。
    猜你喜欢
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2015-02-08
    • 1970-01-01
    • 2017-10-28
    相关资源
    最近更新 更多