2011-07-28 71 views
0

想知道在我的應用程序中成功登錄時如何存儲用戶登錄數據的最佳方式。即此時已登錄時,我做這樣的事情在我的登錄腳本ASP.NET如何在APP中存儲用戶登錄數據

      Session("loggedIn") = "Yes" 
          Session("userName") = reader_login("useremail").ToString() 
          Session("userId") = reader_login("user_ID").ToString() 
          Session("firstName") = reader_login("firstName").ToString() 
          Session("lastName") = reader_login("lastName").ToString() 

然後我用我的劇本,這些會話變量,但只注意到每次我想用一些會話變量我做的在調用它們之前需要檢查它們是否爲null,在很多.aspx頁面中重複這些操作似乎有點笨拙。有一個更好的方法嗎 ?

編輯:

我也很奇怪,爲什麼我需要補充的是在每個腳本會話空檢查我使用的會話變量,我把在母版頁的檢查,但發現我仍然可以在零例外我在我的母版頁中引用的用戶控件,但沒有IS空值檢查

回答

1

您可以通過您放入會話中的單個對象加載它。這將刪除所有的字符串,因爲你可以設置屬性。你也可以檢查對象是否在會話中可用,如果不是用戶沒有登錄?

public class CurrentUserObject 
{ 
    public string UserName { get; set; } 
    public string UserID { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 

    public CurrentUserObject(string userName, string userID, string firstName, string lastName) 
    { 
     UserName = userName; 
     UserID = userID; 
     FirstName = firstName; 
     LastName = lastName; 
    } 
} 

您可以實例化這個對象並將其存儲在Session(「CurrentUser」)或其他東西中。如果您請求此會話變量並且它變爲空,則您的用戶未登錄。我建議您在母版頁中執行此操作以避免重複此代碼。

+0

這聽起來您能不能給會議的一個實例對象 – StevieB

+0

嘿,如果我保存這個會議(「當前用戶」)對象如何我可以檢索即變量名從它? I.e Session(「CurrentUser」)。用戶名?此外,我同意檢查在母版頁空檢查,但我發現,如果我不把我的用戶控件空校驗,即和呼叫會話變量在那裏,如果會話已經死了,我得到零點異常,怎麼來的主頁不能捕捉到? – StevieB

+0

你可能做的檢查主控頁面下旬在你的頁面生命週期。如果您的用戶已被登錄,您可以在構建子控件之前直接重定向。在這裏看到:http://stackoverflow.com/questions/5593652/asp-net-page-page-load-firing-before-master-pages-page-load-event – thekip

-3

可能是你需要在你的應用程序中使用緩存,因爲你要我每次想爲節省使用數據緩存會更好,這裏的時間來檢查,如果空或不是有一些鏈接:

http://msdn.microsoft.com/en-us/library/xsbfdd8c(v=vs.71).aspx

http://msdn.microsoft.com/en-us/library/ms972379.aspx

http://www.exforsys.com/tutorials/asp.net/caching-in-asp.net.html

http://www.codeproject.com/KB/web-cache/cachingaspnet.aspx

希望它有助於標記爲答案,如果它有幫助:)

+0

緩存如何解決會話範圍中的問題?緩存是應用程序範圍。 – thekip

+0

爲什麼要使用用戶認證決定的緩存?這只是誤導和不正確 – archil

+0

對我來說我使用的是nhibernate我正在使用類似會話的東西,但我必須將用戶數據存儲在一個名爲用戶請求的對象中,它將與我在整個應用程序中,這就是爲什麼我告訴 –

0

你不必在會話中存儲「loggedIn」。 你可以使用Session [「userName」]來檢查是否爲空,未登錄;不爲空,登錄。

嘗試使用一個會話項來跟蹤用戶登錄狀態,例如用戶名或用戶標識。

也可以encapsule邏輯成方法如

static bool CheckLogin(HttpSession sessionState, out username, out userId, out firstName, out LastName); 

FYI

1

會話是不檢查用戶是否被認證或不是這樣。清除應用程序池時,管理員可以根據需要清除會話,也可以通過服務器上的低內存清除會話。在這種情況下,您不會希望註銷用戶。在ASP.NET中這樣做的內建和推薦的方法是將數據存儲在身份驗證cookie中。一旦用戶登錄,您發出包含所有數據的cookie,包括用戶ID,名稱等。然後,您不必檢查會話中的每個屬性是否爲null,更簡單 - 您只需檢查用戶通過身份驗證 - 那麼你已經獲得了數據,否則 - 沒有。和其他利益,如果你替換內置主要以自定義的,你可以定義強類型的對象,它擁有從會話對象中提取用戶數據,沒有更多的鑄造。我這裏還有例子定義與窗體身份驗證

首先定義主體,讓我們來定義自定義MyIdentity和MyPrincipal

public class MyIdentity : IIdentity 
    { 
     private FormsAuthenticationTicket _Ticket; 
     private int _userId = 0; 

     public FormsAuthenticationTicket Ticket 
     { 
      get { return _Ticket; } 
     } 

     public string Name 
     { 
      get { return _Ticket.Name; } 
     } 

     public int UserId 
     { 
      get 
      { 
       if (_userId == 0) 
        _userId = Convert.ToInt32(_Ticket.UserData.Split("|".ToCharArray())[0]); 

       return _userId; 
      } 
     } 

     public Identity(FormsAuthenticationTicket ticket) 
     { 
      this._Ticket = ticket; 
     } 

     public string AuthenticationType 
     { 
      get { return "Custom"; } 
     } 

     public bool IsAuthenticated 
     { 
      get { return UserId > 0; } 
     } 
    } 

然後保存MyIdentity

public class MyPrincipal : IPrincipal 
    { 
     private MyIdentity _Identity; 

     public Principal(MyIdentity identity) 
     { 
      _Identity = identity; 
     } 

     public IIdentity Identity 
     { 
      get { return _Identity; } 
     } 

     public bool IsInRole(string role) 
     { 
      return false; 
     } 
    } 

則替換原有形式的用戶與MyPrincipal自定義的一個。 Global.asax

private void Application_OnPostAuthenticateRequest(object sender, EventArgs e) 
    { 
     IPrincipal usr = HttpContext.Current.User; 
     // If we are dealing with an authenticated forms authentication request 
     if (usr.Identity.IsAuthenticated && usr.Identity.AuthenticationType == "Forms") 
     { 
      FormsIdentity formsIdentity = usr.Identity as FormsIdentity; 
      // Create a CustomIdentity based on the FormsAuthenticationTicket    
      IIdentity identity = new MyIdentity(formsIdentity.Ticket); 
      IPrincipal principal = new MyPrincipal(identity); 
      // Attach the CustomPrincipal to HttpContext.User and Thread.CurrentPrincipal 
      HttpContext.Current.User = principal; 
      Thread.CurrentPrincipal = principal; 
     } 
    } 

定義發佈表單認證票證的方法。稍後,自定義MyIdentity類將從userData中提取userId和其他方法。

public static HttpCookie GetAuthCookie(string userName, string userData, bool createPersistentCookie, HttpSessionStateBase session) 
     { 
      HttpCookie authCookie = FormsAuthentication.GetAuthCookie(userName, createPersistentCookie); 
      FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value); 
      FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, userData, session.SessionID); 
      authCookie.Value = FormsAuthentication.Encrypt(newTicket); 

      return authCookie; 
     } 

當用戶檢查並進行身份驗證,返回他們的身份驗證cookie

Response.Cookies.Add(AuthenticationCookie.GetAuthCookie(model.UserName, GetUserInfo(model.UserName, passwordHash), model.RememberMe, Session)); 
//GetUserInfo returns | separated string of user datas. "userId|userName|firstName|lastName" for example. 

,最後,利用上述所有的代碼

if(User.Identity.IsAuthenticated) 
{ 
    int userId = ((MyIdentity)User.Identity).UserId; 
} 

這看起來可能較大代碼,但是在運行時,它將比存儲會話中的所有數據帶來更多好處。它們的主要部分是每次都進行空值檢查和投射。

+0

你有解釋原因的一些參考:'會話不是檢查用戶是否被認證的方式'?我會非常感興趣的閱讀! – thekip

+0

我沒有確切的參考,所以我會盡力解釋它。爲什麼會話總是檢查空值?其中一個原因是因爲你不知道什麼時候可以清除。清除可能是由於回收應用程序池引起的,或者可能是由於服務器內存不足等原因造成的。一旦用戶通過身份驗證,當您清除會話時您肯定不希望失去身份驗證 - 這可能會導致他意外並且不必要的驗證請求每次。會話應該主要填充你能夠恢復的東西,如果它們丟失了,可以再次存儲。 – archil

+0

在你的例子中,用戶將不得不再次登錄。如果應用程序池得到回收,則無論您做什麼(除非使用InProc會話狀態以外的內容),所有會話都會丟失。在session中存儲的東西顯然是在登錄時指定的,所以在Session中存儲它們是非常有意義的。 – thekip

相關問題