重定向登录后返回网址 [英] redirect to return url after login

查看:173
本文介绍了重定向登录后返回网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Razor视图链接是这样的:

I have link on my razor view like this:

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

我的登录操作方法中,我使用这样的:

inside my login 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);

}
}
}

它重定向到: /争端/指数,但它应该去对myApp /争端/指数,其中作为与查询字符串的URL是这样的。 /对myApp /主页/登录?RETURNURL = /争端/指数

it is redirecting to:/Disputes/Index but it should go to myApp/Disputes/Index where as the url with querystring is like this. /myApp/Home/Login?ReturnUrl=/Disputes/Index

我怎样才能解决这个问题呢?

How can I solve this issue?

推荐答案

我使用上述建议的组合和 Request.UrlReferrer 来获得previous位置

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

    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();
    }

这样我就不必把位置在 ActionLink的

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

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

    [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");
        }
    }

这篇关于重定向登录后返回网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆