Login and Maintaining Session


Stored Proecedure:
Create proc [dbo].[usp_cust_login]
(
@mob_no nvarchar (15),
@password nvarchar (50)
)
as
      begin
if(select count(*) from tbl_cust_details where mob_no=@mob_no and password=@password)>0
      begin
      select name,mob_no , emailid from tbl_cust_details where mob_no=@mob_no and password=@password
      end
     
end

//Select name ,mob_no,emailid… is used for picking the values from table for maintaning session.

In Class file
#region cutomer login
  
    public DataSet customer_login(string mob_no, string password)
    {
      
        var con = new SqlConnection(connectionstring());
        con.Open();
        var cmd = new SqlCommand("usp_cust_login", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Clear();

        cmd.Parameters.AddWithValue("@mob_no", mob_no);
        cmd.Parameters.AddWithValue("@password", password);
        SqlDataAdapter dap = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        dap.Fill(ds);
       
        return ds;

    }
    #endregion

On .cs Page

protected void btn_login_Click(object sender, EventArgs e)
    {
        DataSet ds = cs1.customer_login(txt_mobile.Text.Trim(), txt_password.Text.Trim());
      
        if (ds.Tables[0].Rows.Count > 0)
        {
            Session["cust_mob"] = ds.Tables[0].Rows[0]["mob_no"].ToString();
            Session["cust_email"] = ds.Tables[0].Rows[0]["emailid"].ToString();
            Session["cust_name"] = ds.Tables[0].Rows[0]["name"].ToString();
            //Response.Redirect("~/User/Dashboard.aspx?id=" + Guid.NewGuid().ToString().Substring(0, 9));
            Response.Cookies["admin_name"].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies["password"].Expires = DateTime.Now.AddDays(-1);

        }

        else
        {
            txt_password.Text = "";
            txt_mobile.Text = "";

            string msg = string.Empty;
            msg = "Incorrect mobile number or password";
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + msg + "');", true);
               
            return;


        }
    }




Summary
In this article I try to expose Login and Session maintenance. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

No comments:

Post a Comment