Skip to content

Instantly share code, notes, and snippets.

@JayDouglass
Created August 30, 2011 21:28
Show Gist options
  • Save JayDouglass/1182103 to your computer and use it in GitHub Desktop.
Save JayDouglass/1182103 to your computer and use it in GitHub Desktop.
Allows storing and retrieving an object in FormsAuthentication cookie
Imports Newtonsoft.Json
Imports System.Web
' Gets and auth cookie and stores an object in JSON format in userData
' Useful for storing ids, roles information, and email address
Public Class FormsAuthenticationExtensions
' Calls this to get cookie when logging a user in
Public Shared Function GetAuthCookie(ByVal userName As String, ByVal userData As Object, Optional ByVal createPersistentCookie As Boolean = True) As HttpCookie
Dim cookie = FormsAuthentication.GetAuthCookie(userName, createPersistentCookie)
Dim ticket = FormsAuthentication.Decrypt(cookie.Value)
Dim userDataJson = JsonConvert.SerializeObject(userData)
Dim ticketWithUserData = New FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, _
ticket.Expiration, ticket.IsPersistent, userDataJson, _
ticket.CookiePath)
Dim encryptedTicket = FormsAuthentication.Encrypt(ticketWithUserData)
cookie.Value = encryptedTicket
Return cookie
End Function
' Deserializes JSON stored in forms auth cookie userData
' Calls this in Global.asax PostAuthenticate to set principal for the request
Public Shared Function ExtractUserData(Of T)(ByVal identity As System.Security.Principal.IIdentity) As T
Dim formsIdentity = TryCast(identity, FormsIdentity)
If formsIdentity Is Nothing Then
Throw New ArgumentException("identity is not a FormsIdentity")
End If
Return ExtractUserData(Of T)(formsIdentity)
End Function
Public Shared Function ExtractUserData(Of T)(ByVal identity As FormsIdentity) As T
Dim ticket = identity.Ticket
Dim userDataJson = ticket.UserData
Dim userData = JsonConvert.DeserializeObject(Of T)(userDataJson)
If userData Is Nothing Then Throw New ArgumentException("Could not deserialize ticket user data. UserData: " & ticket.UserData)
Return userData
End Function
End Class
' Global.asax
' To retrieve object from cookie and assign it to the request's IPrincipal
Private Sub Application_OnPostAuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
' Don't do anything if this is an unauthenticated request
If Not Context.Request.IsAuthenticated Then Return
'If User Is Nothing OrElse Not User.Identity.IsAuthenticated Then Return
' This principal & identity will flow throughout the request.
' example of retrieving object from ticket:
Dim userData = FormsAuthenticationExtensions.ExtractUserData(Of CosmetologyFormsTicketData)(HttpContext.Current.User.Identity)
Dim principal = CosmetologyFormsAuthentication.GetRequestPrincipal()
' Attach the new principal object to the current HttpContext object
HttpContext.Current.User = principal
' Make sure the Principal's are in sync
System.Threading.Thread.CurrentPrincipal = HttpContext.Current.User
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment