PROGRAM TO REMEMBER USERNAME AND PASSWORD USING COOKIES

DEFAULT.ASPX FILE:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>

    <form id="form1" runat="server">
    User Name:<asp:TextBox ID="txtusername" runat="server"></asp:TextBox><br />
    Password:<asp:TextBox ID="txtpassword" runat="server" TextMode ="Password" ></asp:TextBox><br />
    Remember me:<asp:CheckBox ID="CheckBox1" runat="server" /><br />
    <asp:Button ID="Button1" runat="server" Text="Login" OnClick ="login_click" />
 
    </form>
</body>
</html>

DEFAULT.ASPX.VB FILE:


Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub login_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If CheckBox1.Checked Then
            Response.Cookies("UserName").Expires = DateTime.Now.AddDays(30)
            Response.Cookies("Password").Expires = DateTime.Now.AddDays(30)
        Else
            Response.Cookies("UserName").Expires = DateTime.Now.AddDays(-1)
            Response.Cookies("Password").Expires = DateTime.Now.AddDays(-1)
        End If
        Response.Cookies("UserName").Value = txtusername.Text.Trim
        Response.Cookies("Password").Value = txtpassword.Text.Trim
        MsgBox("welcome", MsgBoxStyle.Exclamation, "welcome")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If ((Not (Request.Cookies("Username")) Is Nothing AndAlso (Not (Request.Cookies("password")) Is Nothing))) Then
                txtusername.Text = Request.Cookies("username").Value
                txtpassword.Attributes("value") = Request.Cookies("password").Value
            End If
        End If
    End Sub
End Class

No comments:

Post a Comment