Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Security.Cryptography
- Imports System.Data.SqlClient
- Imports System.Data
- Public Class commonFunctions
- Public Sub createLoginCookie()
- If HttpContext.Current.Session("loginToken") Is Nothing Then
- Call updateLoginToken()
- Else
- Dim loginCookie As New HttpCookie("evecp")
- loginCookie.Values("userName") = HttpContext.Current.Session("userName")
- loginCookie.Values("loginToken") = HttpContext.Current.Session("loginToken")
- loginCookie.Expires = Date.Now.AddMonths(1)
- HttpContext.Current.Response.Cookies.Add(loginCookie)
- End If
- End Sub
- Public Sub updateLoginToken()
- HttpContext.Current.Session("loginToken") = Guid.NewGuid.ToString
- Using dbConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\evecp.mdf;Integrated Security=True;User Instance=True")
- Using dbCommand As New SqlCommand("UPDATE tblUsers SET loginToken = @loginToken WHERE userName = @userName;")
- dbCommand.Connection = dbConnection
- dbCommand.Parameters.AddWithValue("@loginToken", HttpContext.Current.Session("loginToken"))
- dbCommand.Parameters.AddWithValue("@userName", HttpContext.Current.Session("userName"))
- dbConnection.Open()
- dbCommand.ExecuteNonQuery()
- dbConnection.Close()
- End Using
- End Using
- Call createLoginCookie()
- End Sub
- Public Function retrieveLoginToken(userName As String)
- Using dbConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\evecp.mdf;Integrated Security=True;User Instance=True")
- Using dbAdapter As New SqlDataAdapter("SELECT TOP 1 loginToken FROM tblUsers WHERE userName = @userName;", dbConnection)
- dbAdapter.SelectCommand.Parameters.AddWithValue("@userName", userName)
- Using dtLoginToken As New DataTable
- dbAdapter.Fill(dtLoginToken)
- Return dtLoginToken.Rows(0)(0).ToString
- End Using
- End Using
- End Using
- End Function
- Public Function hashPassword(plainTextPassword As String, passwordSalt As String)
- Dim passwordHasher As New SHA1Managed()
- Dim hashedPassword As String = BitConverter.ToString(passwordHasher.ComputeHash(Encoding.UTF8.GetBytes(plainTextPassword & passwordSalt)))
- Return hashedPassword
- End Function
- Public Sub purgeLogin()
- Dim loginCookie As New HttpCookie("evecp")
- loginCookie.Expires = Date.Now.AddDays(-1)
- HttpContext.Current.Response.Cookies.Add(loginCookie)
- HttpContext.Current.Session.Abandon()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement