Sunteți pe pagina 1din 13

Web Engineering-II

Using Asp.net

BY:
Adnan Amin
Lecturer / Softwar Programmer
Information and Communication Technology (ICT-Dept)
www.geoamins.com
Overview
• Creating Login System using cookies.
• Sign in
• Remember me
• Sign out

2
By: Adnan Amin (Lecturer/Programmer)
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

3
By: Adnan Amin (Lecturer/Programmer)
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

4
By: Adnan Amin (Lecturer/Programmer)
Design the WebForm
• Login.aspx page

• Header location of Main.aspx page

5
By: Adnan Amin (Lecturer/Programmer)
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

6
By: Adnan Amin (Lecturer/Programmer)
Code Behind the Sign out button
• Response.Cookies("user").Expires = Now.AddDays(-1)
OR
• Response.Cookies("user").Values.Remove("uid")
• Response.Redirect(“login.aspx")

7
By: Adnan Amin (Lecturer/Programmer)
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 8
By: Adnan Amin (Lecturer/Programmer)
Next Example about login
• Create a web site project using Microsoft visual
developer.
• Add one web form (if not exists)  main.aspx
• The following Controls;
Control Name Properties to Changed Values
Textbox ID txtUserId

Textbox ID txtPassword

Label Text User Name:

Label Text Password:

Button Text Sign in

Checkbox ID chkRemember

Text Remember me. 9


By: Adnan Amin (Lecturer/Programmer)
Design Main.aspx page

10
By: Adnan Amin (Lecturer/Programmer)
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 11
By: Adnan Amin (Lecturer/Programmer)
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 12
By: Adnan Amin (Lecturer/Programmer)
Thank You! 13
By: Adnan Amin (Lecturer/Programmer)

S-ar putea să vă placă și