Contact Form

Name

Email *

Message *

Login & Logout with cookies and regular expression

No comments

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="EPC2.Login" %>

<!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></title>
    <link href="../css/animate.css" rel="stylesheet" type="text/css" />
    <link href="../css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="../css/responsive.css" rel="stylesheet" type="text/css" />
    <link href="../css/font-awesome.min.css" rel="stylesheet" type="text/css" />
    <link href="../css/main.css" rel="stylesheet" type="text/css" />
    <link href="../css/prettyPhoto.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <section id="form"><!--form-->
<div class="container">
<div class="row">
<div class="col-sm-4 col-sm-offset-4">
<div class="login-form"><!--login form-->
                    <span>
                    <h2 class="title text-center">Admin Login</h2>
</span>
                        <div class="form-group col-md-12">
<asp:TextBox ID="txtUserName" runat="server" placeholder="UserName" class="form-control" ></asp:TextBox>
                          <asp:RequiredFieldValidator ID="RFUserName" runat="server" ErrorMessage="*" ControlToValidate="txtUserName"
                                    ValidationGroup="UP" ForeColor="Red"></asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="REUserName" runat="server" ErrorMessage="*"
                                    Display="Dynamic" ControlToValidate="txtUserName" ValidationExpression="^[0-9 a-z A-Z\- \s]+$"
                                    ValidationGroup="UP" ForeColor="Red"></asp:RegularExpressionValidator>
                        </div>
                         <div class="form-group col-md-12">
                        <asp:TextBox ID="txtPassword" runat="server" placeholder="Password" class="form-control" TextMode="Password"></asp:TextBox>
                         <asp:RequiredFieldValidator ID="RFPassword" runat="server" ErrorMessage="*" ControlToValidate="txtPassword"
                                    ValidationGroup="UP" ForeColor="Red"></asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="REPassword" runat="server" ControlToValidate="txtPassword"
                                    ValidationExpression="^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$"
                                    ErrorMessage="*"
                                    ForeColor="Red" ValidationGroup="UP" />
                        </div>
                        <div class="form-group col-md-12">
                                        <h4><asp:Label ID="lblmessage" runat="server" Text="" class="label label-danger" Visible="false"></asp:Label></h3>
                        <asp:Button ID="btnlogin" runat="server" Text="Login"
                                class="btn btn-primary pull-right" onclick="btnlogin_Click" ValidationGroup="UP"></asp:Button>
</div>
                            <div>
                                      </div>
</div><!--/login form-->
</div>

</div>
</div>
</section>
        <!--/form-->
    </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE_EPC2;
using BL_EPC2;

namespace EPC2
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

           

        }


        protected void btnlogin_Click(object sender, EventArgs e)
        {
            lblmessage.Text = "";
            BEUser be = new BEUser();
            be.Username = txtUserName.Text;
            be.Password = txtPassword.Text;
            var b = new BLUser().show().Where(id => id.Username.ToUpper() == txtUserName.Text.ToUpper() && id.Password.ToUpper() == txtPassword.Text.ToUpper());
            if (b.Count() != 0)
            {
                createcookies(be);
                Response.Redirect("Welcome.aspx");

            }
            else
            {
                lblmessage.Visible = true;
                lblmessage.Text = "Wrong Username Or Password";
            }
        }
        public void createcookies(BEUser bes)
        {
            HttpCookie cook = new HttpCookie("mod");
            cook["Name"] = txtUserName.Text;
            cook["address"] = txtPassword.Text;
            Response.SetCookie(cook);
        }

    }
}

FOR LOGOUT:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EPC2.Admin
{
    public partial class Logout : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cookies["mod"].Expires = DateTime.Now;

        }
    }
}

No comments :

Post a Comment