2012-12-25 51 views
0

我有User表。我使用Forms認證,並具有以下代碼的登錄:如果我從DB中刪除用戶,則用戶保持認證(大會話超時)

public JsonResult LogOn(FormCollection form) 
{ 
     var agent = SiteUserRepository.CheckAgent(form["Email"], form["Password"]); 
     if (agent == null) 
      return Json(new 
      { 
       IsSuccess = false, 
       Message = "Wrong email or password" 
      }); 
     SiteUserRepository.UpdateLastLogon(agent.Email); 

     FormsAuthentication.SetAuthCookie(agent.Email, true); 
     ApplicationSession.CurrentUser = agent; 

     return Json(new 
      { 
       IsSuccess = true 
      }); 

} 

ApplicationSession是我Session對象包裝。

public static class ApplicationSession 
{ 
    private const string _currentUser = "CurrentUser"; 
    public static SiteUser CurrentUser 
    { 
     get 
     { 
      var user = HttpContext.Current.Session[_currentUser]; 
      if (user != null) 
       return (SiteUser)user; 
      return null; 
     } 
     set 
     { 
      HttpContext.Current.Session[_currentUser] = value; 
     } 
    } 

} 

會話超時等於1440(24小時)。我需要這個價值。 例如,用戶在網站上登錄。然後我從數據庫中刪除這個用戶。用戶將被認證(如果他們當然沒有點擊註銷)。解決這個問題的最佳方法是什麼?

回答

1

您想要使用的cookie。你的代碼需要檢查cookie,如果它不存在,或者時間已經過期,你可以創建一個新的cookie。

我認爲這會告訴你一個很好的例子:

C# Cookies based on login information

http://www.beansoftware.com/ASP.NET-Tutorials/Cookies-ASP.NET.aspx

用了一天的期滿記得我複選框類型的實現可以做這樣的事情:

if (chBox.Checked) 

     { 
      HttpCookie myCookie = new HttpCookie("Cookie_Remember"); //new cookie object 

      Response.Cookies.Remove("Cookie_Remember"); //This will remove previous cookie 
      Response.Cookies.Add(Cookie_Remember); //This will create new cookie 

      Cookie_Remember.Values.Add("UserInfo", txt_username.Text); //Add User Name 

     // You can add multiple values 

      DateTime CookieExpir= DateTime.Now.AddDays(5); //Cookie life 

      Response.Cookies["Cookie_Remember"].Expires = CookieExpir; //Maximum day of cookie's life  
     } 
0

解決方法是隻允許用戶通過應用程序從存儲庫/數據庫中刪除用戶。

當用戶被刪除,迫使數出來:

//Delete the user 
SiteUserRepository.DeleteUser(agent); 

//Log them out 
FormsAuthentication.Signout(); 
+0

我不認爲它會起作用。因爲我將用戶從其他應用程序(CMS)中刪除。 – user1260827