2013-04-14 51 views
0

我有一個控制器上Logout action像這樣:ASP.NET MVC註銷與窗體身份驗證

public ActionResult Logout() 
{ 
    FormsAuthentication.SignOut(); 
    Session["UserCredential"] = null; 
    return RedirectToAction("Index", "Home"); 
} 

該工作在谷歌Chrome瀏覽器。但是當我在第一次登錄和註銷後使用我的web應用程序和firefox瀏覽器(最新版本)時。當我再次登錄到應用程序並按下注銷按鈕時,我無法從Web應用程序註銷。 Request.IsAuthenticated正在給我真正的價值。

對於登錄我用以下行動:

 [HttpPost] 
     public JsonResult Login(string userName, string password) 
     { 

      User oUser = oRepository.GetUser(userName,password); 
      Session["UserCredential"] = oUser; 


      if (oUser != null) 
      { 
       if (oUser.IsVerified) 
       { 
        string url = Request.Url.AbsolutePath;  
        FormsAuthentication.SetAuthCookie(userName, false); 
        return Json(new { res = 1, RedirectUrl = Url.Action("Index", "Home") }, JsonRequestBehavior.AllowGet); 
       } 
       else 
       { 
        return Json(new { res = 0, RedirectUrl = "" }, JsonRequestBehavior.AllowGet); 
       } 
      } 

      return Json(new { res = -1, RedirectUrl = "" }, JsonRequestBehavior.AllowGet); 
     } 

任何人有知道我必須做的解決我用firefox瀏覽器的問題。

+0

瀏覽器控制檯中的任何錯誤? –

+1

沒有。實際上在註銷按鈕按它調用主控制器的索引操作。 –

+0

然後你解決了你的問題 –

回答

2

我不是100%確定,但你可以試試這個。

我發現在ASP.NET中實現FormsAuthentication的方式,SignOut方法不能清除ASPXAUTH cookie。所以,在退出時,我通常會做的是手動清除響應中的所有cookie。

你可能會嘗試這樣做。至少,在你的情況下,你應該清除2個cookies:

1)FormsAuth cookie。您可以通過訪問FormsAuthentication類的CookieName(或某些此類)靜態字段來獲取cookie的名稱。

2)ASP.NET會話cookie。檢查立即窗口(打印Cookies集合)中的cookie名稱。

要清除cookie,只需將新cookie添加到響應對象的cookie集合中,其名稱與舊cookie相同,然後將其到期日期設置爲過去的日期。

+0

感謝@Water Cooler v2的幫助。你的答案爲我解決了我的問題。 –