Imports Microsoft.VisualBasic
Imports System.Web.Security
Imports System.Text.RegularExpressions
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Enumerations
Imports CharterDotCom.CommonEntities.Library.EntityMemberServices
Imports CharterDotCom.CommonEntities.Library.EntityEBSServices
Imports CharterDotCom.CommonEntities.Library.EntitySupport
Imports CharterDotCom.CommonEntities.Library.EntitySearch
Imports CharterDotCom.CommonEntities.Library.EntityWebConfig
Imports CharterDotCom.CommonEntities.Library.EntityG2BServices
Imports CharterDotCom.CommonEntities.Library.Common
Imports CharterDotCom.WebSite.Library
' issue 26553

''' <summary>
''' Handle the creation/destruction of NextIT (ask.charter) specific cookies.
''' </summary>
''' <remarks></remarks>
Public Class CookieHandler

    Private Const cUserCookieName As String = "askCh1"
    Private Const cAccountCookieName As String = "askCh2"
    Private Const cCookieLife As Integer = 15
    ''' <summary>
    ''' Encrypt and store two values in secured cookies
    ''' </summary>
    ''' <param name="psUserName">User ID field to store</param>
    ''' <param name="psAccountNumber">User Account Number to store</param>
    ''' <param name="poHttpResponse">Response object containing cookie store</param>
    ''' <remarks></remarks>
    Public Shared Sub StoreCookies(ByVal psUserName As String, _
                                   ByVal psAccountNumber As String, _
                                   ByVal poHttpResponse As HttpResponse, _
                                   ByVal poHttpServer As HttpServerUtility, _
                                   ByVal psDomain As String)

        Dim valueToStore As String = String.Empty
        Dim newCookie As HttpCookie = Nothing

        poHttpResponse.Cookies.Add(BuildCookie(cUserCookieName, psUserName, poHttpServer, psDomain))
        poHttpResponse.Cookies.Add(BuildCookie(cAccountCookieName, psAccountNumber, poHttpServer, psDomain))

    End Sub

    Private Shared Function BuildCookie(ByVal psCookieName As String, _
                                        ByVal psValue As String, _
                                        ByVal poHttpServer As HttpServerUtility, _
                                        ByVal psDomain As String) _
        As HttpCookie

        Dim valueToStore As String = String.Empty
        Dim newCookie As HttpCookie = Nothing
        Dim sDomain As String = String.Empty

        valueToStore = poHttpServer.UrlEncode(EncryptionHandler.Encrypt(psValue))
        newCookie = New HttpCookie(psCookieName, valueToStore)
        newCookie.Secure = True
        ' TESTING NOTE:  Internet Explorer has a known issue when running LOCALHOST
        ' it will not store cookies with a domain property.  This can only be tested
        ' from the server.
        If (psDomain IsNot Nothing) Then
            sDomain = psDomain
        End If

        newCookie.Domain = sDomain
        newCookie.Expires = DateTime.Now.AddMinutes(cCookieLife)

        Return newCookie
    End Function

    ''' <summary>
    ''' Reset the expiration time for the cookie based on user activity.
    ''' </summary>
    ''' <param name="poHttpResponse">Http Response object containing cookies</param>
    Public Shared Sub ResetCookieTimer(poHttpResponse As HttpResponse, _
                                       ByVal poHttpRequest As HttpRequest, _
                                        ByVal psDomain As String)
        UpdateOneCookie(poHttpResponse, poHttpRequest, cUserCookieName, psDomain)
        UpdateOneCookie(poHttpResponse, poHttpRequest, cAccountCookieName, psDomain)
    End Sub

    Private Shared Sub UpdateOneCookie(ByVal poHttpResponse As HttpResponse, _
                                       ByVal poHttpRequest As HttpRequest, _
                                       ByVal psCookieName As String, _
                                       ByVal psDomain As String)
        If poHttpResponse.Cookies(psCookieName) IsNot Nothing Then
            poHttpResponse.Cookies(psCookieName).Secure = True
            poHttpResponse.Cookies(psCookieName).Domain = psDomain
            poHttpResponse.Cookies(psCookieName).Value = poHttpRequest.Cookies(psCookieName).Value
            poHttpResponse.Cookies(psCookieName).Expires = DateTime.Now.AddMinutes(cCookieLife)
        End If
    End Sub

    ''' <summary>
    ''' Manual removal of cookies is required when the session timeout is reached
    ''' </summary>
    ''' <param name="poHttpResponse">Response object containing cookies</param>
    ''' <remarks></remarks>
    Public Shared Sub ClearCookies(ByVal poHttpResponse As HttpResponse, _
                                ByVal poHttpRequest As HttpRequest, _
                                ByVal psDomain As String)
        With poHttpResponse.Cookies(cUserCookieName)
            .Secure = True
            .Domain = psDomain
            .Expires = DateTime.Now.AddDays(-1)
        End With
        With poHttpResponse.Cookies(cAccountCookieName)
            .Secure = True
            .Domain = psDomain
            .Expires = DateTime.Now.AddDays(-1)
        End With

    End Sub
End Class