2012-12-11 25 views
-1

这里是我的登录控制器登录到MVC ASP.Net应用程序后,重定向到returnurl的正确方法是什么?

 // 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/ 

我想是在该URL为http:本地主机:8085/MobileApprover因为当我在那个URL类型(调试时)家庭控制器正确显示图。

我在这里做错了什么?我相信这个问题是关系到返回URL以及如何我处理它,但我试图

return Redirect(ReturnURL) 

但这并不工作。

+0

我不明白为什么这是downvoted? – Pseudonym

回答

0

我终于清理了我的代码略有并能够得到

Redirect(ReturnUrl); 

工作,或者你可以使用

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

对于登出功能

相关问题