+ All Categories
Home > Documents > Lesson_13 Cookies-Login

Lesson_13 Cookies-Login

Date post: 08-Apr-2018
Category:
Upload: adnan-amin
View: 217 times
Download: 0 times
Share this document with a friend

of 13

Transcript
  • 8/7/2019 Lesson_13 Cookies-Login

    1/13

  • 8/7/2019 Lesson_13 Cookies-Login

    2/13

    Overview Creating Login System using cookies.

    Sign in Remember me Sign out

    By: Adnan Amin (Lecturer/Programmer)

    2

  • 8/7/2019 Lesson_13 Cookies-Login

    3/13

    Requirement First create new web site project using visual

    studio.net or visual web developer. Then add two Web forms in web site.

    Login.aspx Main.aspx

    By: Adnan Amin (Lecturer/Programmer)

    3

  • 8/7/2019 Lesson_13 Cookies-Login

    4/13

    Controls used Login.aspx

    Button (ID) btnSignin (text) Sign in Label (ID) lblUsername (text) Username

    Label (ID) lblPassword (text) Password Textbox (ID) txtUsername (text) txtUsername Textbox (ID) txtPassword (text) txtPassword Checkbox (ID) chkRemember (text) Remember me.

    Main.aspx Label (ID) lblUser (text) Welcome User Button (ID) btnSignout (text) Sign out

    By: Adnan Amin (Lecturer/Programmer)

    4

  • 8/7/2019 Lesson_13 Cookies-Login

    5/13

    Design the WebForm Login.aspx page

    Header location of Main.aspx page

    By: Adnan Amin (Lecturer/Programmer)

    5

  • 8/7/2019 Lesson_13 Cookies-Login

    6/13

    Code Behind the Sign in button

    If chkRemember.Checked then Dim abc As HttpCookie = New HttpCookie("user") abc.Values("uid") = TextBox1.Text abc.Expires = Now.AddDays(1) Response.Cookies.Add(abc) Response.Redirect("main.aspx")

    End If

    By: Adnan Amin (Lecturer/Programmer)

    6

  • 8/7/2019 Lesson_13 Cookies-Login

    7/13

    Code Behind the Sign out button

    Response.Cookies("user").Expires = Now.AddDays(-1)OR

    Response.Cookies("user").Values.Remove("uid") Response.Redirect(login.aspx")

    By: Adnan Amin (Lecturer/Programmer)

    7

  • 8/7/2019 Lesson_13 Cookies-Login

    8/13

    Code Behind the Main_Load Open the Main.aspx page Double click on Main.aspx page in design view. Type the following codes in Main_Load event; If Not IsPostBack Then 'Check if the browser support cookies If Request.Browser.Cookies Then 'Check if the cookies with name user exist on user's machine If Request.Cookies("user") IsNot Nothing Then Me.lblUser.Text = Request.Cookies("user")("uid")

    End If End If End If

    By: Adnan Amin (Lecturer/Programmer)

    8

  • 8/7/2019 Lesson_13 Cookies-Login

    9/13

  • 8/7/2019 Lesson_13 Cookies-Login

    10/13

    Design Main.aspx page

    By: Adnan Amin (Lecturer/Programmer)

    10

  • 8/7/2019 Lesson_13 Cookies-Login

    11/13

    Code Behind Sign in button. If Button9.Text = "Sign in" Then Dim abc As HttpCookie = New HttpCookie("user") abc.Values("uid") = TextBox1.Text abc.Expires = Now.AddDays(1) Response.Cookies.Add(abc) Button9.Text = "Sign out" Response.Redirect("main.aspx")

    ElseIf Button9.Text = "Sign out" Then If Not Response.Cookies("user") Is Nothing Then 'Response.Cookies("user").Expires = Now.AddDays(-1) Response.Cookies("user").Values.Remove("uid") Button9.Text = "Sign in" End If End If

    By: Adnan Amin (Lecturer/Programmer)

    11

  • 8/7/2019 Lesson_13 Cookies-Login

    12/13

    Code Bihind the Main_Load Event If Not IsPostBack Then 'Check if the browser support cookies If Request.Browser.Cookies Then

    'Check if the cookies with name user exist on user's machine If Request.Cookies("user") IsNot Nothing Then 'Pass the user name and password to the VerifyLogin method Me.Label7.Text = Request.Cookies("user")("uid") If Request.Cookies("user")("uid") IsNot Nothing Then Button9.Text = "Sign out" End If End If End If End If

    By: Adnan Amin (Lecturer/Programmer)

    12

  • 8/7/2019 Lesson_13 Cookies-Login

    13/13

    Thank You!

    By: Adnan Amin (Lecturer/Programmer)

    13


Recommended