Search code examples
asp.netvb.netfunctionsharedpublic

Is it safe to use a Shared property for authentication verification?


Should I be using Shared, or just a Public function and initiate a class when needed? I have read a number of articles, but still cant get my head around the best option here.

Here is the code I want to get. The userid in question is a string that's set when the user logs in. As I may need the userid in a number of pages, I want to add it in a class.

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

Solution

  • I would almost never say that you should leverage a Shared member when dealing with authentication - but since you're using Cookies I can't see any reason this code wouldn't work as is and make it easier to access.