Step 6
Finally we need to make sure that when the user clicks the login button the
code in Step 5 executes.
1. Switch to Design view and highlight the Login button.
2. Switch to Code view and add the following pink attribute to the asp:button code:
<asp:Button ID="btn_login" Text="Login" onClick="button_click" runat="server" />
This will execute the Sub Routine named button_click when the button is clicked.
Protect Page
Step 1
Let's create the code for a page that you don't want to users to access unless
they are logged in.
1. Switch to Code view and place the following code right underneath
the page directive.
<Script runat="server">
Sub Page_Load(s as object, e as eventargs)
Dim objckusername
As HttpCookie
objckusername = Request.Cookies("ckusername")
If
objckusername Is Nothing Then
Response.Redirect("login.aspx")
Else
lblckusername.Text
= Server.HtmlEncode(objckusername.Value)
End
If
End Sub
</script>
In C#:
<script runat="server">
void Page_Load(Object s, EventArgs e)
{
HttpCookie objckusername = Request.Cookies["ckusername"];
if (objckusername == null)
{
Response.Redirect("login.aspx");
}
else
{
lblckusername.Text = Server.HtmlEncode(objckusername.Value);
}
}
</script>
Note
The code above checks to see if the cookie named ckusername is empty; if it is, we redirect the
user to the login page, and if it's not, then we stuff the value of the cookie ckusername into
a variable so we could use it to display on our page.
2. You can now display the value of the cookie anywhere on the page with this
code:
<asp:label id="lblckusername" runat="server" />
Create Logout
Step 1
Let's create the code to logout the user. We need to create a new page that
will delete the cookie when accessed.
1. Create a new page and name it logout.aspx.
2. Switch to Code view and place the following code right underneath the page
directive.
<script runat="server">
Sub Page_Load(s as object, e as eventargs)
Dim objckusername
As HttpCookie
objckusername
= Request.Cookies("ckusername")
If objckusername
Is Nothing Then
'if
cookie does not exist user is not logged in
'and
shouldn't be accessing this page anyhow - send user away
Response.Redirect(Request.ServerVariables("HTTP_REFERER"))
Else   'if cookie
exists user is logged in and wants to logout so kill cookie
objckusername.Expires
= DateTime.Now.AddDays(-1)
Response.Cookies.Add(objckusername)
Response.Redirect(Request.ServerVariables("HTTP_REFERER"))
End If
End Sub
</script>
In C#:
<script runat="server">
void Page_Load(Object s, EventArgs e)
{
HttpCookie objckusername = Request.Cookies["ckusername"];
if (objckusername == null)
{
/* if cookie does not exist user is not logged in
and shouldn't be accessing this page anyhow - send user
away */
Response.Redirect(Request.ServerVariables["HTTP_REFERER"]);
}
else
/*if cookie exists, user is logged in, wants to logout,
so kill cookie */
{
objckusername.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(objckusername);
Response.Redirect(Request.ServerVariables["HTTP_REFERER"]);
}
</script>
Starting out as a fine artist, Omar Elbaga gradually moved to computer graphic arts. He was particularly amazed by the power of the World Wide Web, so he embarked upon building small-scale sites for fun utilizing HTML and his Art background. Falling in love with designing web pages and its potential, he began a career in web design. Omar has since been in the web development field for several years. With his head in computer books nearly 24 hours a day, Omar moved on to enhance his skills from web design to web programming.
Most of his work involves building database-driven web sites for small companies. Omar is currently running a popular Dreamweaver MX resource site named dmxfire.com
See All Postings From Omar Elbaga >>