Return to Snippet

Revision: 46116
at May 13, 2011 01:40 by derebus


Initial Code
Partial Class _Default
Inherits System.Web.UI.Page
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
'Check if the browser support cookies
If Request.Browser.Cookies Then
'Check if the cookies with name PBLOGIN exist on user's machine
If Request.Cookies("PBLOGIN") IsNot Nothing Then
'Pass the user name and password to the VerifyLogin method
Me.VerifyLogin(Request.Cookies("PBLOGIN")("UNAME").ToString(), Request.Cookies("PBLOGIN")("UPASS").ToString())
End If
End If
End If
End Sub
 
Protected Sub BtLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'check if remember me checkbox is checked on login
If (Me.CbRememberMe.Checked) Then
'Check if the browser support cookies
If (Request.Browser.Cookies) Then
'Check if the cookie with name PBLOGIN exist on user's machine
If (Request.Cookies("PBLOGIN") Is Nothing) Then
'Create a cookie with expiry of 30 days
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(30)
'Write username to the cookie
Response.Cookies("PBLOGIN").Item("UNAME") = Me.TbUserName.Text
'Write password to the cookie
Response.Cookies("PBLOGIN").Item("UPASS") = Me.TbPassword.Text
'If the cookie already exist then wirte the user name and password on the cookie
Else
Response.Cookies("PBLOGIN").Item("UNAME") = Me.TbUserName.Text
Response.Cookies("PBLOGIN").Item("UPASS") = Me.TbPassword.Text
End If
End If
End If
 
Me.VerifyLogin(Me.TbUserName.Text, Me.TbPassword.Text)
End Sub
 
Protected Sub VerifyLogin(ByVal UserName As String, ByVal Password As String)
Try
'If login credentials are correct
'Redirect to the user page
'else
'prompt user for invalid password
'end if
Catch ex as System.Exception
Response.Write(ex.Message)
End Try
End Sub
 
Protected Sub lbSignout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbSignout.Click
'Check iIf the cookies with name PBLOGIN exist on user's machine
If (Request.Cookies("PBLOGIN") IsNot Nothing) Then
'Expire the cookie
Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(-30)
End If
 
'Redirect to the login page
End Sub
End Class

Initial URL
http://www.daniweb.com/web-development/aspnet/threads/30505

Initial Description
How To Create A Remember Me Cookie

Initial Title
How To Create A Remember Me Cookie

Initial Tags
login, aspnet

Initial Language
VB.NET