Imports Microsoft.VisualBasic
Imports System.Xml
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Imports System.Text
Imports System.Collections.Generic


<Serializable()> _
Public Class CMSUserToken
 Public _userid As Int32
 Public _roles() As String

' Converts a xml user token into a CMSUserToken object
  Public Shared Function LoadUserFromTicket(ByVal ticketXml As String) As CMSUserToken
  '     extract binary data from string by tokenization
        Dim data() As String = ticketXml.Split(".")
        Dim buffer As New List(Of Byte)
        For Each item As String In data
            buffer.Add(item)
        Next


        Dim stream As New MemoryStream(buffer.ToArray())
        Dim _bf As BinaryFormatter = New BinaryFormatter()
        Dim user As Object = _bf.Deserialize(stream)
        Return CType(user, CMSUserToken)
  End Function

'Serializes a CMSUserToken object to xml
Public Shared Function SerializeToTicket(ByVal token As CMSUserToken) As String
    Dim _bf As BinaryFormatter = New BinaryFormatter()
    Dim stream As New MemoryStream
    _bf.Serialize(stream, token)
    Dim buffer() As Byte = stream.GetBuffer()
    Dim sb As New StringBuilder
    Dim first As Boolean = True

'   create string by including tokenization markers
    For Each item As Byte In buffer
        If first Then
            sb.Append(item)
            first = False
        Else
            sb.Append("." & item)
        End If

    Next
    Dim retval As String = sb.ToString()
    Return retval
End Function




End Class
