Creating a Custom Membership Provider and Membership User utilizing
a Data Set Table Adapter - Step 11
by Nannette Thacker
Our Cookie Handler class.
When we log in to the site, we optionally set a Remember Me cookie. This cookie is checked each time the Master Page is loaded, so that if the user is not logged in, they are auto-logged into the web site. Below are our cookie handling classes:
Our AppCookie.vb class is a generic handler for all cookies. Our SSSCookie.vb class is specifically for the current application.
AppCookie
Imports Microsoft.VisualBasic
Public Class AppCookie
' vb.net cookie methods
' use for any and all cookies to, get, and delete values
' http://msdn2.microsoft.com/en-us/library/ms178194(vs.80).aspx
' http://msdn2.microsoft.com/en-us/library/system.web.httpcookie(VS.80).aspx
Public Sub AppCookie()
End Sub
' pass in number of days til expiration or 0 for non persistent
Public Sub SetCookie(ByVal CookieName As String, ByVal Value As String, ByVal CookieTimeoutInDays As Integer)
Dim cookie As HttpCookie
cookie = New HttpCookie(CookieName, Value)
If CookieTimeoutInDays <> 0 Then
cookie.Expires = DateTime.Now.AddDays(CookieTimeoutInDays)
End If
If HttpContext.Current.Response.Cookies(CookieName) Is Nothing Then
HttpContext.Current.Response.Cookies.Add(cookie)
Else
HttpContext.Current.Response.Cookies.Set(cookie)
End If
End Sub
' overload method, if pass in false value, sets to default 365 days, if true, makes non persistent
Public Sub SetCookie(ByVal CookieName As String, ByVal Value As String, ByVal NonPersistent As Boolean)
If Not NonPersistent Then
Dim CookieTimeoutInDays As Integer = 365
SetCookie(CookieName, Value, CookieTimeoutInDays)
Else
SetCookie(CookieName, Value, 0)
End If
End Sub
' overload method, pass in name and value, defaults to non persistent
Public Sub SetCookie(ByVal CookieName As String, ByVal Value As String)
SetCookie(CookieName, Value, 0)
End Sub
Public Function GetCookie(ByVal CookieName As String) As String
Dim cookie As HttpCookie
cookie = HttpContext.Current.Request.Cookies(CookieName)
If Not cookie Is Nothing Then
GetCookie = HttpContext.Current.Request.Cookies(CookieName).Value
Else
GetCookie = ""
End If
End Function
Public Sub DeleteCookie(ByVal CookieName As String)
If Not HttpContext.Current.Response.Cookies(CookieName) Is Nothing Then
HttpContext.Current.Response.Cookies(CookieName).Expires = DateTime.Now.AddDays(-1D)
End If
End Sub
Public Function Displaycookies() As String
' nkt
' http://www.codetoad.com/asp.net/cookies.asp
' original example in C#, rewrite in VB
Dim cookies As HttpCookieCollection
cookies = HttpContext.Current.Request.Cookies
Dim cookie As HttpCookie
Displaycookies = "" ' default, loop could return null
For n As Integer = 0 To cookies.Count - 1
cookie = cookies(n)
Displaycookies = Displaycookies & "<hr/>Name: <b>" & cookie.Name & "</b><br />"
Displaycookies = Displaycookies & "Expiry: " & cookie.Expires.ToString() & "<br />"
Displaycookies = Displaycookies & "Domain: " + cookie.Domain + "<br />"
Displaycookies = Displaycookies & "Value: " + cookie.Value + "<br />"
Next
End Function
Public Sub DeleteAllCookies()
' delete all cookies
Dim cookies As HttpCookieCollection
cookies = HttpContext.Current.Request.Cookies
Dim cookie As HttpCookie
For n As Integer = 0 To cookies.Count - 1
cookie = cookies(n)
SetCookie(cookie.Name, "", -10)
Next
End Sub
Public Function DeleteAllCookiesDisplay() As String
' nkt delete all cookies and return names of cookies deleted...
' used for debugging
Dim cookies As HttpCookieCollection
cookies = HttpContext.Current.Request.Cookies
Dim cookie As HttpCookie
Dim cookienames As String = ""
For n As Integer = 0 To cookies.Count - 1
cookie = cookies(n)
SetCookie(cookie.Name, "", -10)
cookienames += cookie.Name & "<br />"
Next
DeleteAllCookiesDisplay = cookieNames
End Function
End Class
SSSCookie
Imports Microsoft.VisualBasic
Imports Appcookie
Public Class SSSCookie
Inherits System.Web.UI.Page
Public Sub SSSCookie()
' nothing goes here
End Sub
Shared cookieObj As AppCookie = Nothing
Public Sub DeleteCookies()
If cookieObj Is Nothing Then
cookieObj = New AppCookie()
End If
' clear all cookies
cookieObj.DeleteAllCookies()
cookieObj.DeleteCookie("SSSCheckSum")
cookieObj.DeleteCookie("SSSUserID")
cookieObj.DeleteCookie("RememberMe")
End Sub
Public Sub SetUserCookies(ByVal UserID As String, ByVal CookieTimeoutInDays As Integer)
If cookieObj Is Nothing Then
cookieObj = New AppCookie()
End If
cookieObj.SetCookie("UserID", UserID, CookieTimeoutInDays)
End Sub
Public Function GetUserID() As Decimal
'nkt: for checking if have log in cookie....
If cookieObj Is Nothing Then
cookieObj = New AppCookie()
End If
Dim UserID As String = cookieObj.GetCookie("UserID")
If String.IsNullOrEmpty(UserID) Then
UserID = "0"
End If
GetUserID = CDec(UserID)
End Function
Public Sub SetRememberMeCookie(ByVal Value As String)
If cookieObj Is Nothing Then
cookieObj = New AppCookie()
End If
cookieObj.SetCookie("RememberMe", Value)
End Sub
Public Function GetRememberMeCookie() As String
If cookieObj Is Nothing Then
cookieObj = New AppCookie()
End If
GetRememberMeCookie = cookieObj.GetCookie("RememberMe")
End Function
Public Function GetCheckSumCookie() As String
If cookieObj Is Nothing Then
cookieObj = New AppCookie()
End If
GetCheckSumCookie = cookieObj.GetCookie("CheckSum")
End Function
Public Sub SetCheckSumCookie(ByVal Value As String, ByVal CookieTimeoutInDays As Integer)
If cookieObj Is Nothing Then
cookieObj = New AppCookie()
End If
cookieObj.SetCookie("CheckSum", Value, CookieTimeoutInDays) ' store for 365 days...
End Sub
Public Sub SetCheckSum(ByVal UserID As String, ByVal Username As String, ByVal UserPassword As String, ByVal CookieTimeoutInDays As Integer)
' nkt: create the checksum value and save as a cookie
Dim checksumstr As String
checksumstr = CreateCheckSum(UserID, Username, UserPassword)
SetCheckSumCookie(checksumstr, CookieTimeoutInDays)
End Sub
Public Function CompareCheckSum(ByVal UserID As String, ByVal Username As String, ByVal UserPassword As String) As Boolean
' nkt: compare the cookie value with the actual value...
Dim checksumsuccess As Boolean = False
Dim checksum As String
checksum = GetCheckSumCookie() ' get the cookie value
If Not String.IsNullOrEmpty(checksum) Then ' if not blank
If CreateCheckSum(UserID, Username, UserPassword) = CLng("0" & checksum) Then
checksumsuccess = True ' if valid
End If
End If
CompareCheckSum = checksumsuccess
End Function
' http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_10237356.html?sfQueryTermInfo=1+checksum
' getChecksum as found on EE....
Function GetChecksum(ByVal Source As String) As Long
Dim iVal, Weight, CheckHold, CheckSum As Long
Weight = 1
CheckSum = 0
For iVal = 1 To Len(Source)
CheckHold = Asc(Mid$(Source, iVal, 1)) * Weight
CheckSum = CheckSum + CheckHold
Weight = Weight + 2
Next iVal
GetChecksum = CheckSum Mod &H7FFFFFFF
End Function
Public Function CreateCheckSum(ByVal UserID As String, ByVal Username As String, ByVal UserPassword As String) As Long
Dim checkusernumber As Long = 0
Dim checkpasswordnumber As Long = 0
checkusernumber = GetChecksum(Username)
checkpasswordnumber = GetChecksum(UserPassword)
CreateCheckSum = UserID * checkusernumber + (checkpasswordnumber * 587)
End Function
#Region "SetCookies"
Public Sub SetCookies(ByVal userID As Decimal, ByVal userName As String, ByVal userPassword As String)
Dim RememberMe As String
RememberMe = GetRememberMeCookie()
' if "true" then save the log in cookie values....
Dim CookieTimeoutInDays As Integer
If CStr(RememberMe) = "true" Then
' expire in 365 days
Dim myCookies As New AppCookie()
myCookies.DeleteCookie("RememberMe") ' delete the cookie
CookieTimeoutInDays = 365
Else
CookieTimeoutInDays = 0
End If
SetUserCookies(userID, CookieTimeoutInDays)
SetCheckSum(userID, userName, userPassword, CookieTimeoutInDays)
End Sub
#End Region
End Class
We are now ready to begin
Step 12.
Steps for Creating a Custom Membership Provider and Membership
User utilizing a DataSet Table Adapter:
Introduction: Creating a Custom Membership Provider and Membership User utilizing
a DataSet Table Adapter.
Step 1: Creating the Project and Folders.
Step 2: Table Data Structure and Web.config for this Tutorial.
Step 3: Creating The DataSet.
Step 4: Creating the Table Adapter Methods for GetUserByLogin, GetUserByUserName, and InsertUser.
Step 5: Creating our Custom MembershipProvider Class.
Step 6: Adding Properties to Our Custom MembershipProvider Class.
Step 7: Creating Our Custom MembershipUser Class.
Step 8: Customizing our MembershipProvider Class.
Step 9: Our Custom RoleProvider Class.
Step 10: Our Master page.
Step 11: Our Cookie Handler class.
Step 12: Our Log In page.
Step 13: Our Register page.
Step 14: Our Log Out page.
Step 15: Our Change Password page.
Step 16: Our Administration page.
Step 17: Loading a Menu Programmatically based on Roles.
Step 18: Our Default page.
Download the ZIP files:
VB: ShiningStarCustomMemberProviderTutorial.zip