我應該使用Shared
還是隻使用Public
函數並在需要時啓動一個類?我已經閱讀了一些文章,但仍然無法讓我的頭在這裏最好的選擇。使用共享屬性進行身份驗證驗證安全嗎?
這是我想要得到的代碼。有問題的userid
是用戶登錄時設置的字符串。由於我可能需要多個頁面中的用戶標識,因此我想將其添加到類中。
Public Shared ReadOnly Property userid As String
Get
Dim ck As HttpCookie = HttpContext.Current.Request.Cookies(FormsAuthentication.FormsCookieName)
Dim tkt As FormsAuthenticationTicket = FormsAuthentication.Decrypt(ck.Value)
Return tkt.Name
End Get
End Property
關鍵是調用操作的上下文。在這種情況下,該屬性從會話值中讀取,因此它可以正常工作。情況並非總是如此。看看幾年前我問過的這個問題:http://stackoverflow.com/questions/4026785/how-do-static-properties-work-in-an-asp-net-enviroment – asawyer 2013-04-09 20:19:56