asp.net-mvc 登录后重定向到返回 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9554115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:46:57  来源:igfitidea点击:

redirect to return url after login

asp.net-mvcasp.net-mvc-3

提问by DotnetSparrow

I have a link in my razor view like this:

我的剃刀视图中有一个链接,如下所示:

 <a href="Home/Login?ReturnUrl=Disputes/Index"> disputes</a>

Inside my login's action method, I am using this:

在我的登录操作方法中,我使用了这个:

 public ActionResult Login(string returnUrl) {
   if (string.IsNullOrEmpty(returnUrl) && Request.UrlReferrer != null)
         returnUrl = Server.UrlEncode(Request.UrlReferrer.PathAndQuery);

   if (Url.IsLocalUrl(returnUrl) && !string.IsNullOrEmpty(returnUrl))
   {
      ViewBag.ReturnURL = returnUrl;
   }

   return View();
 }

In view I am using this:

鉴于我正在使用这个:

 @Html.Hidden("returnUrl",@Request.QueryString)

Then in post action method:

然后在后期操作方法中:

 public ActionResult LogOn(LogOnModel model, string returnUrl)
 {
   if (ModelState.IsValid)
   {
      if (membershipService.ValidateUser(model.UserName, model.Password, model.Type))
      {
         formsAuthenticationService.SignIn(model.UserName, model.RememberMe);
         SetUserInfo(model.UserName);

         string decodedUrl = "";
         if (!string.IsNullOrEmpty(returnUrl))
            decodedUrl = Server.UrlDecode(returnUrl);

         if (Url.IsLocalUrl(decodedUrl))                    
            return Redirect(decodedUrl);
         else
            return Redirect("Home", Index);

      }
   }
 }

It is redirecting to:/Disputes/Indexbut it should go to myApp/Disputes/Indexwhere the url with query string is like this. /myApp/Home/Login?ReturnUrl=/Disputes/Index

它正在重定向到:/Disputes/Index但它应该转到myApp/Disputes/Index带有查询字符串的 url 的位置。/myApp/Home/Login?ReturnUrl=/Disputes/Index

How can I solve this issue?

我该如何解决这个问题?

回答by Oliver

I use a combination of the above suggestion and Request.UrlReferrerto get the previous location:

我使用上述建议的组合并Request.UrlReferrer获取以前的位置:

    public ActionResult LogOn(string returnUrl)
    {
        //So that the user can be referred back to where they were when they click logon
        if (string.IsNullOrEmpty(returnUrl) && Request.UrlReferrer != null)
            returnUrl = Server.UrlEncode(Request.UrlReferrer.PathAndQuery);

        if (Url.IsLocalUrl(returnUrl) && !string.IsNullOrEmpty(returnUrl))
        {
            ViewBag.ReturnURL = returnUrl;
        }
        return View();
    }

This way I don't have to put the location in the ActionLink.

这样我就不必将位置放在ActionLink.

I populate a hidden field in the login page using the ViewBag.ReturnURL. Then in the Login HTTPPost ActionResultI redirect the user to the location in the hidden field (if there is one):

我使用ViewBag.ReturnURL. 然后在登录 HTTPPost 中,ActionResult我将用户重定向到隐藏字段中的位置(如果有):

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        //returnURL needs to be decoded
        string decodedUrl = "";
        if (!string.IsNullOrEmpty(returnUrl))
            decodedUrl = Server.UrlDecode(returnUrl);

        //Login logic...

        if (Url.IsLocalUrl(decodedUrl))
        {
            return Redirect(decodedUrl);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }

回答by Darin Dimitrov

In this case you could have your LogOn action take a returnUrl parameter and if it is not empty, instead of redirecting to Home/Index you could return Redirect(returnUrl);. Take a look at the default AccountController generated by VS when you create a new project. It does exactly that.

在这种情况下,您可以让您的 LogOn 操作采用 returnUrl 参数,如果它不为空,您可以 return 而不是重定向到 Home/Index Redirect(returnUrl);。看一下新建项目时VS生成的默认AccountController。它正是这样做的。

回答by Ravi Ganesan

If the ReturnURLis null, make sure you are calling the action method from the view as follows:

如果ReturnURLnull,请确保您正在从视图中调用操作方法,如下所示:

// FormMethod.post is optional
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post))
{
    // login view html
}

Account controller:

帐户控制器:

[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
    ViewBag.ReturnUrl = returnUrl;
    return View();
}

回答by ?brahim Aya??büyük

        //Utilities         
        public static ReturnUrl GetReturnUrlValues(string returnUrl)
        {
            //0:Action,1:Controller,2:Area:,3:Id
            ReturnUrl vm = new ReturnUrl();
            using (TBBSEntities context = new TBBSEntities())
            {
                if (returnUrl != null && returnUrl.Length > 10)
                {
                    var counterValue = returnUrl.Split('/');
                    vm.Action = counterValue[0];
                    vm.Controller = counterValue[1];
                    vm.Area = counterValue[2] == "0" ? "" : counterValue[2] ;
                    vm.Id = Convert.ToInt32(counterValue[3]);
                    vm.NullStatus = true;
                    return vm;
                }
                vm.NullStatus = false;
                return vm;
            }
        }

        //Controller
        var transformUrl = Utilities.GetReturnUrlValues(returnUrl);
            if (transformUrl.NullStatus)
            {
            return RedirectToAction(transformUrl.Action, transformUrl.Controller,
                    new { area = transformUrl.Area, id = transformUrl.Id });
            }
            return RedirectToAction("Action", "Controller", new { area = "Area", Id});


        //View
        var returnUrl = "Action/Controller/Area/" + @Model.Id;