the following code is my login page using stored proc.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=ATSSERVER\\SQLEXPRESS;Initial Catalog=arun;User ID=sa;Password=mis@12345");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("chkusr",con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter s1 = new SqlParameter("@username", TextBox1.Text);
SqlParameter s2 = new SqlParameter("@password", TextBox2.Text);
cmd.Parameters.Add(s1);
cmd.Parameters.Add(s2);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
Response.Write("Login Success");
}
else
{
Response.Write("Login failed");
}
}
}
SenthilkumarPosted Apr 27, 2012, 4:23 AM
Instead of store the login crendentials into session variable, you can use the base page which will be base class to all the web pages in the application.
You can check the condition there and session expired then you can redirect into the login page.
if(Session["Login"] == null)
{
Response.Redirect("Login.aspx");
}
After logged in successfully, you can set the session as Login
Session["Login"] = "LoggedIn";
Response.Redirect("home.aspx");