Posted By

derebus on 05/13/11


Tagged

login cookie aspnet ME remember


Versions (?)

How To Create A Remember Me Cookie


 / Published in: VB.NET
 

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

How To Create A Remember Me Cookie

  1. Partial Class _Default
  2. Inherits System.Web.UI.Page
  3.  
  4. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5. If Not IsPostBack Then
  6. 'Check if the browser support cookies
  7. If Request.Browser.Cookies Then
  8. 'Check if the cookies with name PBLOGIN exist on user's machine
  9. If Request.Cookies("PBLOGIN") IsNot Nothing Then
  10. 'Pass the user name and password to the VerifyLogin method
  11. Me.VerifyLogin(Request.Cookies("PBLOGIN")("UNAME").ToString(), Request.Cookies("PBLOGIN")("UPASS").ToString())
  12. End If
  13. End If
  14. End If
  15. End Sub
  16.  
  17. Protected Sub BtLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  18. 'check if remember me checkbox is checked on login
  19. If (Me.CbRememberMe.Checked) Then
  20. 'Check if the browser support cookies
  21. If (Request.Browser.Cookies) Then
  22. 'Check if the cookie with name PBLOGIN exist on user's machine
  23. If (Request.Cookies("PBLOGIN") Is Nothing) Then
  24. 'Create a cookie with expiry of 30 days
  25. Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(30)
  26. 'Write username to the cookie
  27. Response.Cookies("PBLOGIN").Item("UNAME") = Me.TbUserName.Text
  28. 'Write password to the cookie
  29. Response.Cookies("PBLOGIN").Item("UPASS") = Me.TbPassword.Text
  30. 'If the cookie already exist then wirte the user name and password on the cookie
  31. Else
  32. Response.Cookies("PBLOGIN").Item("UNAME") = Me.TbUserName.Text
  33. Response.Cookies("PBLOGIN").Item("UPASS") = Me.TbPassword.Text
  34. End If
  35. End If
  36. End If
  37.  
  38. Me.VerifyLogin(Me.TbUserName.Text, Me.TbPassword.Text)
  39. End Sub
  40.  
  41. Protected Sub VerifyLogin(ByVal UserName As String, ByVal Password As String)
  42. Try
  43. 'If login credentials are correct
  44. 'Redirect to the user page
  45. 'else
  46. 'prompt user for invalid password
  47. 'end if
  48. Catch ex as System.Exception
  49. Response.Write(ex.Message)
  50. End Try
  51. End Sub
  52.  
  53. Protected Sub lbSignout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbSignout.Click
  54. 'Check iIf the cookies with name PBLOGIN exist on user's machine
  55. If (Request.Cookies("PBLOGIN") IsNot Nothing) Then
  56. 'Expire the cookie
  57. Response.Cookies("PBLOGIN").Expires = DateTime.Now.AddDays(-30)
  58. End If
  59.  
  60. 'Redirect to the login page
  61. End Sub
  62. End Class

Report this snippet  

You need to login to post a comment.