1

我在設置自定義表單身份驗證票據後重定向到安全操作時遇到問題。這裏是正在發生的事情:ASP.Net MVC 4自定義授權票據重定向問題

  1. 我瀏覽到網站/首頁/指數
  2. 我自動重定向到網站/帳號/登錄
  3. 我有一個有效的用戶登錄/密碼
  4. 的RedirecToUrl( )函數試圖我重定向到站點/主頁/指數,但我會自動返回到網站/帳號/登錄
  5. 請求被驗證。如果我手動導航到網站/家庭/索引,我允許英寸

任何人都可以擺脫任何光線?

我的HomeController:

[Authorize] 
public ActionResult Index() 
{ 
    return View(); 
} 

我的AccountController:

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

    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public ActionResult Login(LoginModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      bool bLogin = MyAuthentication.Login(model.UserName, model.Password); 

      if (bLogin) 
      { 
       Response.Cookies.Add(MyAuthentication.GetAuthenticationCookie(model.UserName.ToLower(), model.RememberMe)); 
       RedirectToUrl(returnUrl); 
      } 
      else 
       ModelState.AddModelError("", "That is not a valid Username/Password combination"); 

     } 

     return View(model); 
    } 

    private ActionResult RedirectToUrl(string returnUrl) 
    { 
     if (Url.IsLocalUrl(returnUrl)) 
      return Redirect(returnUrl); 
     else 
      return RedirectToAction("Index", "Home"); 
    } 

這是我如何創建自定義車票(只是增加用戶數據):

public static HttpCookie GetAuthenticationCookie(string UserName, bool persistLogin) 
    { 
     var userData = null; // Code removed for brevity 

     FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
       1, 
       UserName, 
       DateTime.Now, 
       DateTime.Now.AddMinutes(20), 
       persistLogin, 
       userData); 

     string encTicket = FormsAuthentication.Encrypt(authTicket); 
     return new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); 
    } 

回答

3

唉!

RedirectToUrl(returnUrl); 

需要是

return RedirectToUrl(returnUrl);