Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 1st, 2011  |  syntax: VB.NET  |  size: 2.77 KB  |  views: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Imports System.Security.Cryptography
  2. Imports System.Data.SqlClient
  3. Imports System.Data
  4.  
  5. Public Class commonFunctions
  6.     Public Sub createLoginCookie()
  7.         If HttpContext.Current.Session("loginToken") Is Nothing Then
  8.             Call updateLoginToken()
  9.         Else
  10.             Dim loginCookie As New HttpCookie("evecp")
  11.             loginCookie.Values("userName") = HttpContext.Current.Session("userName")
  12.             loginCookie.Values("loginToken") = HttpContext.Current.Session("loginToken")
  13.             loginCookie.Expires = Date.Now.AddMonths(1)
  14.             HttpContext.Current.Response.Cookies.Add(loginCookie)
  15.         End If
  16.     End Sub
  17.  
  18.     Public Sub updateLoginToken()
  19.         HttpContext.Current.Session("loginToken") = Guid.NewGuid.ToString
  20.         Using dbConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\evecp.mdf;Integrated Security=True;User Instance=True")
  21.             Using dbCommand As New SqlCommand("UPDATE tblUsers SET loginToken = @loginToken WHERE userName = @userName;")
  22.                 dbCommand.Connection = dbConnection
  23.                 dbCommand.Parameters.AddWithValue("@loginToken", HttpContext.Current.Session("loginToken"))
  24.                 dbCommand.Parameters.AddWithValue("@userName", HttpContext.Current.Session("userName"))
  25.                 dbConnection.Open()
  26.                 dbCommand.ExecuteNonQuery()
  27.                 dbConnection.Close()
  28.             End Using
  29.         End Using
  30.         Call createLoginCookie()
  31.     End Sub
  32.  
  33.     Public Function retrieveLoginToken(userName As String)
  34.         Using dbConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\evecp.mdf;Integrated Security=True;User Instance=True")
  35.             Using dbAdapter As New SqlDataAdapter("SELECT TOP 1 loginToken FROM tblUsers WHERE userName = @userName;", dbConnection)
  36.                 dbAdapter.SelectCommand.Parameters.AddWithValue("@userName", userName)
  37.                 Using dtLoginToken As New DataTable
  38.                     dbAdapter.Fill(dtLoginToken)
  39.                     Return dtLoginToken.Rows(0)(0).ToString
  40.                 End Using
  41.             End Using
  42.         End Using
  43.     End Function
  44.  
  45.     Public Function hashPassword(plainTextPassword As String, passwordSalt As String)
  46.         Dim passwordHasher As New SHA1Managed()
  47.         Dim hashedPassword As String = BitConverter.ToString(passwordHasher.ComputeHash(Encoding.UTF8.GetBytes(plainTextPassword & passwordSalt)))
  48.         Return hashedPassword
  49.     End Function
  50.  
  51.     Public Sub purgeLogin()
  52.         Dim loginCookie As New HttpCookie("evecp")
  53.         loginCookie.Expires = Date.Now.AddDays(-1)
  54.         HttpContext.Current.Response.Cookies.Add(loginCookie)
  55.         HttpContext.Current.Session.Abandon()
  56.     End Sub
  57. End Class