2014-04-23 87 views
0

我在博客上發佈了關於SessionsCookies的信息。下面是詳細介紹保持MVC中會話中的用戶信息不安全

會議

  1. 會話更安全
  2. 會議是在服務器上

餅乾

  1. Cookies是在客戶端
  2. 不太安全
  3. 一旦禁用瀏覽器就很難使用。

在上述論證的基礎上,我用在登錄系統的會話保持UserId,UserName & roleName

現在角色名的基礎上,我將決定是這是Admin進入到管理員節與否。

我已經使用這個代碼在模型MVC

public bool LoginMe() 
     { 
     Int64 Error; 
       //create db 
       Database db = DatabaseFactory.CreateDatabase("DBContext"); 

       DbCommand dbCommand = db.GetStoredProcCommand("ValidateUser"); 

       db.AddInParameter(dbCommand, "@Username", DbType.String, this.UserName); 
       db.AddInParameter(dbCommand, "@Password", DbType.String, EncryptPassword(this.Password)); 
       db.AddOutParameter(dbCommand, "@Error", DbType.Int64, 10); 
       DataSet dsResult = db.ExecuteDataSet(dbCommand); 
       Error = Convert.ToInt64(db.GetParameterValue(dbCommand, "@Error")); 
       if (Error == 1100) 
{ 
    try 
    { 
     var query = (from o in dsResult.Tables[0].AsEnumerable() 
         select new AllUser 
         { 
          UserId = o.Field<int>("UserId"), 
          UserName = o.Field<string>("UserName"), 
          roleName = o.Field<string>("roleName"), 
         }).Single(); // this will raise an exception if there isn't just one record returned 

     Session["UserId"] = query.UserId; 
     Session["UserName"] = query.UserName; 
     Session["roleName"] = query.roleName; 

     return true; 
    } 
    catch { 
    // do nothing and let method return false as something has gone wrong. 
    // add logging here if you are using it to show there has been a problem 
    } 
    } 
    return false; 
    } 

我用它在瀏覽像@Session["UserId"]

現在像

If you aren't using https and securing the session cookie then this might make it easy to hack your site, although that's the same for any session based site (nearly all of them) 
It might be nice to add some check so that if you remove a user's rights, the session variables are deleted the next time that user requests something from the server, 
otherwise they could carry on using the site even though their account it banned.You'd  have to decide if this is likely and then how you want to do this (using an authorization filter maybe.) 

上述評論這方面的專家意見搞糊塗了。任何機構能否清楚說明?保存這些信息的最佳方法是什麼?

回答

1

會話狀態使用客戶端票證來識別服務器端會話,它可能容易受到會話ID欺騙和注入攻擊。

因此,要破解會話值,需要黑客入侵遠程服務器。

是的,對於高度安全的應用程序(如網上銀行)使用https。

http://msdn.microsoft.com/en-us/magazine/cc163730.aspx#S9

安全套接字層(SSL),應使用防止會話ID,認證門票,應用餅乾,和其它請求/響應信息的網絡級嗅探。

Can session value be hacked?

0

使用HTTPS,如果你的應用程序處理敏感信息(信用卡號,賬戶NUM,密碼)。 在會話中存儲用戶對象(具有userId,用戶名,角色的模型),而不是單獨的屬性 爲SESSION_ID設置setHttpOnly屬性。

在調用每個操作以反映存儲在數據庫中的當前權限之前,刷新存儲在會話中的User對象可能會很昂貴。