我应该使用Shared
,还是仅使用Public
函数并在需要时启动一个类?我读过很多文章,但仍然无法理解这里的最佳选择。
这是我想要得到的代码。所涉及的 userid
是用户登录时设置的字符串。由于我可能在多个页面中需要该 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
最佳答案
我几乎从来不会说在处理身份验证时应该利用 Shared
成员 - 但由于您使用的是 Cookies
我看不出这段代码的任何原因不会按原样工作并使其更易于访问。
关于asp.net - 使用共享属性进行身份验证是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15911682/