Wednesday 11 December 2019

Login page example...

sample
********************** login-reports.aspx  *******************

<%@ Page Title="" Language="C#" MasterPageFile="~/Master.master" AutoEventWireup="true" CodeFile="login-reports.aspx.cs" Inherits="login-reports" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head1" runat="Server">

<title> page Title  </title>
    <style>
        .big-header .sub-nav li a {
            padding: 20px 28px;
        }
    </style>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="pageContent1" runat="Server">
   
    <div class="container">
        <div class="row">

            <div class="col-sm-4 col-sm-offset-4">
                <div style="margin: 100px 0 100px 0; border: 1px solid #ddd; padding: 20px">
                    <h2 style="text-align: center">Sign In</h2>
                    <br />
                    <div class="form-horizontal">
                        <div class="form-group">
                            <label class="control-label col-sm-3">User ID</label>
                            <div class="col-sm-9">
                                <asp:TextBox ID="txtUserID" runat="server" CssClass="form-control" ></asp:TextBox>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-sm-3">Password</label>
                            <div class="col-sm-9">
                                <asp:TextBox ID="txtPassword" TextMode="Password" runat="server" CssClass="form-control" ></asp:TextBox>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-offset-3 col-sm-9">
                                <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" CssClass="btn btn-danger" />
                                <!--<span style="padding-left: 20px"><a href="/recover-password">Forget Password</a></span>-->
                                <br />
                                <br />
                            </div>
                        </div>
                    </div>
                    <asp:Literal ID="ltrMsg" runat="server"></asp:Literal>
                </div>
            </div>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer1" runat="Server">
</asp:Content>


********************** login-reports.cs *******************


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtUserID.Focus();
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        //string strCon = @"data source=sharedmssql5.znetindia.net,1234;initial catalog=Database_Name;integrated security=False;user id=user_id;password=san$ec12r214;";


        string strCon = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
        SqlConnection con = new SqlConnection(strCon);
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from ClientDetails where ClientID=@username and Password=@word", con);
            cmd.Parameters.AddWithValue("@username", txtUserID.Text);
            cmd.Parameters.AddWithValue("@word", txtPassword.Text);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            int i = cmd.ExecuteNonQuery();

            if (dt.Rows.Count > 0)
            {
                Response.Cookies["clientid"].Value = txtUserID.Text;  //catch cookies....

                Response.Redirect("/research/recommendations");
            }

            else
            {
                ltrMsg.Text = "Invalid credentials".GetMsgError();
            }
            //con.Close();
        }

        catch (Exception ex)
        {
            ex.ToString();
        }
        finally { con.Close(); }
       
        //using (var ctx = new DAEntities())
        //{
        //    var user = ctx.ClientDetails.FirstOrDefault(a => a.UserID == txtUserID.Text.Trim() && a.Password == txtPassword.Text.Trim());
       
        //    if (user != null)
        //    {
        //        // if (txtUserID.Text == "demo" && txtPassword.Text == "san123")
        //        // {
        //        Response.Cookies["clientid"].Value = txtUserID.Text;
        //        Response.Redirect("/research/san_Default ");
        //    }
        //    else
        //    {
        //        ltrMsg.Text = "Invalid credentials".GetMsgError();
        //    }
        //}

    }
}




Catch Data to other page /after logout

********************** san_Default .cs *******************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class san_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var cId = Request.Cookies["clientid"];
            if (Request.Cookies["clientid"] == null)
            {
                Response.Redirect("/login-reports?redirecturl=/research/recommendations");
            }
        }
    }


    protected void logout(object sender, EventArgs e)
    {
        Response.Cookies.Clear();

        Session.Abandon();
        Response.Cookies["clientid"].Expires = DateTime.Now.AddDays(-1);
        Response.Cookies["clientid"].Expires = DateTime.Now;
        Response.Redirect("/login-reports");
    }

}





No comments:

Post a Comment