2016-09-16 30 views
0

我想在我的MVC應用程序中實現表單授權,並且大部分它工作正常。但是,當我觸發我的Logout()方法時,什麼都不會發生。我有IsAuthenticated即使在註銷後也是如此()

System.Web.HttpContext.Current.User.Identity.IsAuthenticated

在我的主頁,所以我可以看到,它仍然是登錄之後。 以下是我的註銷方法。

public ActionResult Logout(){ 
     TempData.Clear(); 

     FormsAuthentication.SignOut(); 
     Session.Abandon(); 

     HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, ""); 
     cookie1.Expires = DateTime.Now.AddYears(-1); 
     Response.Cookies.Add(cookie1); 

     HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", ""); 
     cookie2.Expires = DateTime.Now.AddYears(-1); 
     Response.Cookies.Add(cookie2); 

     HttpContext.User =new GenericPrincipal(new GenericIdentity(string.Empty), null); 

     return RedirectToAction("Index", "Home"); 
    } 

---編輯---

我已經加入了HttpContext.GetOwinContext().Authentication.SignOut(DefaultA‌​uthenticationTypes.A‌​pplicationCookie);

如建議通過DGibbs,但問題仍然存在。

+0

對不起,有人說有什麼不對的格式,現在加入。 –

+0

您使用的是什麼版本的MVC?我設法使用'FormsAuthentication.SignOut();'複製你的問題,但是使用:AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);'爲我做了訣竅。 – DGibbs

+0

我的AuthenticationManager根本沒有任何功能? :/ –

回答

0

好的,我發現了錯誤。

如果有其他人遇到這個問題,他們已經嘗試了人們說的話,試着檢查項目的屬性,顯然我已經啓用了WindowsAuth並禁用了AnonymousAuth,但是一旦我切換它的工作就像一個魅力。

enter image description here

相關問題