2013-04-15 18 views
0

我正在使用表單身份驗證爲我正在建設的網站,它工作得很好,除非我無法獲得身份驗證cookie刪除或過期。我已經嘗試了許多方法,但他們都沒有工作。這是我創建cookie的原因。ASP.NET Formauthentication.Signout()不起作用

FormsAuthentication.SetAuthCookie(model.userName, false); 

        HttpCookie cookie = new HttpCookie("user"); 
        Response.Cookies["user"].Value = model.userName; 

現在第二cookie是不是一個實際的authcookie,其用於一些每個客戶端請求的網站的內部工作。接下來的部分是我嘗試刪除Cookie的各種事情。

FormsAuthentication.SignOut(); 
     Roles.DeleteCookie(); 
     Session.Clear(); 
     //Response.Cache.SetExpires(DateTime.Now); 
     //foreach (var cookie in Request.Cookies.AllKeys) 
     //{ 
     // Request.Cookies.Remove(cookie); 
     //} 
     //foreach (var cookie in Response.Cookies.AllKeys) 
     //{ 
     // Response.Cookies.Remove(cookie); 
     //} 
     //Session.Abandon(); 

     //// clear authentication cookie 
     HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, ""); 
     cookie1.Path = FormsAuthentication.FormsCookiePath; 
     cookie1.Expires = DateTime.Now.AddYears(-1); 
     Response.Cookies.Add(cookie1); 

     HttpCookie cookie = Request.Cookies["user"]; 
     string userName = cookie.Value; 
     cookie.Expires.AddDays(-30); 

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

     //HttpCookie cookie = Request.Cookies["user"]; 
     //cookie.Expires = DateTime.Now.AddDays(-1d); 
     //HttpCookie user = Request.Cookies["user"]; 
     //role.Expires = DateTime.Now.AddDays(-1d); 
     //Response.Cookies["user"].Value = null; 
     Session.Abandon(); 

我只是剪切和粘貼在那裏整個事情,有一些是現在註釋掉但在某些時候和時間,我已經嘗試使用各種方法中的代碼刪除的cookie。其中一些嘗試只是猜測,因爲我已經有一段時間了。最後這裏是我的web.config的身份驗證部分

<authentication mode="Forms" > 
    <forms loginUrl="~/login" timeout="90" name=".ASPXFORMS" /> 
</authentication> 

任何輸入,以我做錯了讚賞。

回答

0

嘗試調用

FormsAuthentication.SignOut() 

呼叫會話之前。我通常把它放在一個單獨的方法中,以方便從多個地方撥打電話。類似這樣的:

internal void SignOut(HttpContext context) 
{ 
    FormsAuthentication.SignOut(); 

    HttpSessionState session = context.Session; 

    if(session != null) 
    { 
      session.Abandon(); 
    } 
}