繁体   English   中英

验证用户身份后将用户重定向回上一页

[英]redirect user back to previous page after authenticating user

在验证用户身份后,我似乎无法弄清楚如何返回上一页。 我尝试了return RedirectToLocal(returnUrl),但是它总是将用户带回到主主页索引页面。 当我使用return Redirect(returnUrl)时,它将重新加载上一页。 但是,上一页在ViewBag中的对象此时为空。 我只希望用户返回到上一个页面,保留该状态,将所有输入的数据保留在购物车结帐页面上。

您如何将它们手动或通过属性重定向到“登录”页面? [Authorize]添加到控制器类是最简单的方法,并且默认情况下按预期运行。 例如

[Authorize] public class TestController : Controller

对于您的情况,您可能需要将Authorize属性放在Action而不是整个控制器上

[Authorize]
public ActionResult ConfirmShoppingCart()

这种方法的好处是可以进一步定制

可能的不得已的方法是在呈现“继续”按钮(或想要触发身份验证的任何内容)的视图上手动放置条件。

例如

@if (Request.IsAuthenticated) {
//normal proceed'

} else {
     @Html.ActionLink("Proceed", "Login", "Account", new { @returnUrl = Url.Action("ViewCart", "ShoppingCart")}, new { } )
}

使用关注::

  [HttpPost]
        public ActionResult Login(User model, string returnUrl)
        {
            // Lets first check if the Model is valid or not

            if (ModelState.IsValid)
            {
                using (DbEntities entities = new DbEntities())
                {
                    string username = model.UserID;
                    string password = model.Password;

                    var UserAuth = db.Users.Where(x => x.UserID == model.UserID && x.Password == model.Password).FirstOrDefault();

                    // Now if our password was enctypted or hashed we would have done the
                    // same operation on the user entered password here, But for now
                    // since the password is in plain text lets just authenticate directly

                    bool userValid = entities.Users.Any(user => user.UserID == username && user.Password == password);
                    var usr = entities.Users.FirstOrDefault(x => x.UserID == username && x.Password == password);
                    if (userValid)
                    {
                        //System.Web.HttpContext.Current.Session["_SessionCompany"] = usr.DefaultCompanyID;

                        FormsAuthentication.SetAuthCookie(username, false);
                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            //return RedirectToAction("Index", "Dossier");
                            return RedirectToAction("Index", "Home");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "The user name or password provided is incorrect.");
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM