登录MVC ASP.Net应用程序后重定向到returnurl的正确方法是什么?

时间:2012-12-11 16:54:51

标签: asp.net-mvc authentication asp.net-mvc-routing

这是我的登录控制器

        // POST: /Account/Login

    [AllowAnonymous]
    [HttpPost]

    public ActionResult Login(LoginModel model, string ReturnUrl)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (Url.IsLocalUrl(ReturnUrl))
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    return RedirectToAction("Login", "Login");
                }
            }
            else
            {
                ModelState.AddModelError("invalid", "The user name or password provided is incorrect.");

            }
        }

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

当我开始调试时,这是登录页面上的网址:

http://localhost:8085/MobileApprover/Login/Login?ReturnUrl=%2fMobileApprover%2f&AspxAutoDetectCookieSupport=1 

这是我用来发布的表单

<form class="form" action="/MobileApprover/Login/Login?ReturnUrl=<%=Request.QueryString["ReturnUrl"]%>" method="post">

<input type="text" class="span12" name="UserName" placeholder="User Name" />
<input type="password" class="span12" name="Password" class="" placeholder="Password"/>

<label class="checkbox"> 
    <input type="checkbox" name="RememberMe" class="" /> 
Remember Me? 
</label>                   
<button type="submit"">Login</button>        
       <%:
          Html.ValidationSummary()   
       %> 

登录后我就在这个网址:

 http://localhost:8085/MobileApprover/Login/Login?ReturnUrl=%2fMobileApprover%2f&AspxAutoDetectCookieSupport=1#/MobileApprover/Login/Login?ReturnUrl=/MobileApprover/

我想在这个网址http:localhost:8085 / MobileApprover,因为当我输入该网址时(调试时),家庭控制器正确显示视图。

此时我做错了什么?我相信这个问题与返回网址以及我如何处理它有关,但我试图

return Redirect(ReturnURL) 

但这也不起作用。

1 个答案:

答案 0 :(得分:0)

我最终稍微清理了我的代码并且能够获得

Redirect(ReturnUrl);

工作,或者你可以使用

            Session.Clear();
        FormsAuthentication.SignOut();
        return RedirectToAction("Index", "Home"); 

用于退出功能