2016-10-31 73 views
2

驗證我有一個ASP.NET MVC 4刪除用戶在ASP.NET MVC

我們有一個用戶管理,允許用戶編輯/刪除其他用戶開發的網絡系統。 關於刪除功能,目前我只在數據庫上做了delete

因此,這裏是我的login控制器/方法:

[HttpPost] 
public ActionResult Login(LoginViewModel loginViewModel) 
{ 
    if (_loginService == null) 
     _loginService = new LoginService(); 

    var result = _loginService.Login(loginViewModel.User, loginViewModel.Password); 
    if (!result.Error) 
    { 
     var userData = JsonConvert.SerializeObject(result.User); 
     FormsAuthentication.SetAuthCookie(result.User.Id, false); 
     var ticket = new FormsAuthenticationTicket(1, result.Id, DateTime.Now, DateTime.Now.AddMinutes(9999), true, userData, FormsAuthentication.FormsCookiePath); 
     var encryptedCookie = FormsAuthentication.Encrypt(ticket); 
     var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookie) { Expires = DateTime.Now.AddHours(14) }; 

     Response.Cookies.Add(cookie); 
    } 
    return new JsonResult 
    { 
     Data = result 
    }; 
} 

我對待與一些JavaScript在客戶端上的是回報。到目前爲止,這工作得很好。

對於每個需要認證的用戶,我有[Authorize]屬性。

可以說我剛剛登錄了用戶ABC。只要ABC cookie是活着的,他可以導航罰款..問題是,當某個用戶(可以說ZXC)刪除用戶ABC,他仍然會導航罰款,直到cookie過期。

有沒有辦法在IIS上刪除ABC會話ZXC從數據庫中刪除他? 我不知道..強制cookie過期。我只是不想爲在導航中完成的每個操作執行一次諮詢,以檢查用戶在數據庫中是否仍然「活着」。

任何想法,建議?

回答

2

首先,沒有。在另一個會話中無法訪問cookie,因爲它們只存在於請求/響應的整個生命週期中。但是,您可以存儲所有當前已通過身份驗證的用戶的靜態List,並以這種方式使其失效。

這是有點問題,因爲在應用程序池回收的情況下 - 所有用戶都將「註銷」。如果這不是你的問題(即應用程序池回收在凌晨2點,這是一個商業系統,不凌晨2點運行),那麼你可以試試這個...提供

代碼是未經測試

源:https://msdn.microsoft.com/en-us/library/system.web.security.formsauthenticationmodule.authenticate

編輯:
我沒有去除從請求該cookie並在響應到期它。

在全球範圍內。ASAX

private static List<string> _authenticatedUsers = new List<string>(); 

public static AuthenticateUser (MyApplicationUser user) 
{ 
    if(!_authenticatedUsers.ContainsKey(user.Username)) 
    { 
     _authenticatedUsers.Add(user.Username); 
    } 
} 

public static DeauthenticateUser (MyApplicationUser user) 
{ 
    if(_authenticatedUsers.ContainsKey(user.Username)) 
    { 
     _authenticatedUsers.Remove(user.Username); 
    } 
} 

public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args) 
{ 
    if (FormsAuthentication.CookiesSupported) 
    { 
    if (Request.Cookies[FormsAuthentication.FormsCookieName] != null) 
    { 
     try 
     { 
     FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(
      Request.Cookies[FormsAuthentication.FormsCookieName].Value); 

     MyApplicationUser user = JsonConvert.DeserializeObject(ticket.UserData); 

     if(user == null || !_authenticatedUsers.Any(u => u == user.Username)) 
     { 
      // this invalidates the user 
      args.User = null; 
      Request.Cookies.Remove(FormsAuthentication.FormsCookieName); 
      HttpCookie myCookie = new HttpCookie(FormsAuthentication.FormsCookieName); 
      DateTime now = DateTime.Now; 

      myCookie.Value = "a"; 
      myCookie.Expires = now.AddHours(-1); 

      Response.Cookies.Add(myCookie); 
      Response.Redirect(FormsAuthentication.LoginUrl); 
      Resonpse.End(); 
     } 
     } 
     catch (Exception e) 
     { 
     // Decrypt method failed. 
     // this invalidates the user 
     args.User = null; 
     Request.Cookies.Remove(FormsAuthentication.FormsCookieName); 
     HttpCookie myCookie = new HttpCookie(FormsAuthentication.FormsCookieName); 
     DateTime now = DateTime.Now; 

     myCookie.Value = "a"; 
     myCookie.Expires = now.AddHours(-1); 

     Response.Cookies.Add(myCookie); 
     Response.Redirect(FormsAuthentication.LoginUrl); 
     Resonpse.End(); 
     } 
    } 
    } 
    else 
    { 
    throw new HttpException("Cookieless Forms Authentication is not " + 
          "supported for this application."); 
    } 
} 

在你的登錄操作

public ActionResult Login(LoginViewModel loginViewModel) 
{ 
    ... 

    if (!result.Error) 
    { 
     ... 
     MvcApplication.AuthenticateUser(result.User); 
     ... 
    } 
    ... 
} 

在你註銷操作

public ActionResult Logout(...) 
{ 
    ... 
    MvcApplication.DeauthenticateUser(user); 
    ... 
} 

在你刪除方法

... 
MvcApplication.DeauthenticateUser(user); 
... 
+0

感謝您的時間。我認爲這是一個辦法..但我仍然不知道是否有任何其他選項...我知道,在Java中,我們可以用'JMX interface'這樣做...必須有成纔有可能在C#。 –

+0

您可以創建自己的會話處理程序並枚舉它們 - 但這幾乎就是'List '正在做的事情。 Cookies不存在服務器端,因此不可能改變一個不是當前請求 –

+0

有閒來無事的一部分?我的意思是沒有辦法''formauthentication.signout()'傳遞'id'例如?到'signout()'遠程用戶? –