Join Dotnetcodes DotnetCodes.com is online Discussion Forum for Software professionals . It lets you find friends around the world and Create professional network that share similar interests as you. Get help on ur projects by industry specialists. Also get answers to all ur technical/placement related querries.Get an edge over others.
Already MemberClick here to login
ASP.net MVC Interview Questions Answers Interview Questions
Serial Number in SSRS Articles
How to Print a Crystal Report direct to printer Articles
Get Started Developing for Android Apps with Eclipse Articles
Razor View Engine Interview Questions Answers Interview Questions
.Net framework 4.0 Interview Questions Answers Interview Questions
SQL server reporting services Interview Questions (SSRS) part 1 Articles
Whats New in ASP.NET 4.0 Part 2 Articles
Difference between Encapsulation and Abstraction Interview Questions
Explaining SDLC -System Development Life Cycle Articles
SPIRE PDF Library Articles
Infosys Interview Questions Interview Questions
Html5 interview questions and answers Interview Questions
Dynamic Menu using HTML List Tag and CSS in ASP.Net Articles
SharePoint 2010 interview Questions Answers Interview Questions
Submit Articles | More Articles..

creating a remember me cookie in ASP.net using vb.

Posted By: Jaishree On:12/6/2010 6:42:38 AM in:Source Codes Category:VB Hits:4492
This Article I explain you how to create a remember me cookie using a checkbox that will help to remember user ID but will also allow it to be destroy if a different user ID wants to login with the same computer.

  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

comments powered by Disqus
User Profile
Jaishree
Sr. Engineer
Delhi , India
Email :You must Log In to access the contact details.
Latest Post from :Jaishree
Function to Convert Text String to Title Case – Proper Case
View: 903 | Submitted on: 3/6/2013 11:23:44 PM
Uploading an image directly into the database and retreive in the PictureBox control in Windows Forms Application
View: 2733 | Submitted on: 9/21/2012 3:05:29 AM
Oracle Interview Questions part 2
View: 1867 | Submitted on: 1/7/2011 3:45:40 AM
Oracle Interview Questions part 1
View: 1875 | Submitted on: 1/7/2011 3:43:40 AM
Oracle Interview Questions
View: 1939 | Submitted on: 1/7/2011 3:36:49 AM
Submit Articles | All Post of This User..


Advertise About Us Private Policy Terms of use
All rights reserved to dotnetcodes. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
Best viewed at 1024 x 768 resolution with Internet Explorer 5.0 or Mozila Firefox 3.5 or Google Crome and higher