【筆記】ASP.NET Identity 2.2.0 AuthenticationManager.SignOut(String[]) 方法失效。

  • 891
  • 0
  • 2016-05-30

在使用 ASP.NET Identity 2.2.0 以 Cookie 儲存已登入使用者資訊的 ASP.NET MVC 5 網站應用程式中呼叫 AuthenticationManager.SignOut(); 方法進行使用者登出時,有時會失效。

 

  在使用 ASP.NET Identity 2.2.0 以 Cookie 儲存已登入使用者資訊的 ASP.NET MVC 5 網站應用程式中呼叫 AuthenticationManager.SignOut(); 方法進行使用者登出時,有時會失效;程式執行無發生例外狀況,但使用者並無正常登出。

 

// POST: /Account/LogOff
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
        AuthenticationManager.SignOut();
        return RedirectToAction("Index", "Home");
}

 

  在無更新 ASP.NET Identity 的情況下,將呼叫方法變更為 AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); 即可正常進行使用者登出的動作。

 

// POST: /Account/LogOff
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        return RedirectToAction("Index", "Home");
}

 

  在以 ASP.NET Identity 2.2.1 建立的 MVC 預設範本,在 LogOff 的 Action 中就會在 SingOut 方法傳入 DefaultAuthenticationTypes.ApplicationCookie 的參數。

 

參考資料:

創用 CC 授權條款 本著作由Zhi-Wei製作,以創用CC 姓名標示-非商業性-相同方式分享 4.0 國際 授權條款釋出。