session state with user login2

N

nasirmajor

dear all,
a simple quetion from still a bigginer in asp.net
i have a simple webpage with username and password which is linked with
database. now when user gives
his username and password and if information is right he does move to
the required webpage. but i want
the session to start with his successful login. how to store his id in
session and start and end it when
logout is clicked.
please any small code sample that can help me.or any good relevent
article to this quetion.
thankyou in advance

my aspx file code is as follows:-

<script runat="server">
SqlConnection objconn=new SqlConnection("user
id=sa;password=;Initial Catalog=jobsdb;Data Source=localhost;Integrated

Security=SSPI;");


SqlDataReader objDR;


bool CheckCredentials(string identity2, string Password)
{
objconn.Open();
SqlCommand objcmd=new SqlCommand("SELECT username2,password2
from tbl_login where username2=@username2",objconn);
objcmd.Parameters.Add(new SqlParameter("@username2",
SqlDbType.VarChar, 50));
objcmd.Parameters["@username2"].Value = TextBox1.Text;


objDR = objcmd.ExecuteReader();


if (!objDR.Read())
{
return false;
}
else
{
string strstoredpassword=(string)objDR["password2"];
string strgivenpassword=TextBox2.Text;


return strstoredpassword==strgivenpassword;


}


}
protected void Button1_Click(object sender, EventArgs e)
{
if (CheckCredentials(TextBox1.Text, TextBox2.Text) == true)
{
string url = "cpanelhome.aspx";
Response.Redirect(url);


}
else
{
Label1.Text = "You entered the wrong credentials!";
}


}
</script>


textbox1 is for username
textbox2 is for password


which works correctly. i want session addition in this code.
thankyou again in advance.
 
G

Guest

Hi
When the user is successfully authenticated store in a session variable
session("userid") = userid

When the user logs out abandon the session. But, you need to check for the
session value on every page load. You can use forms authentication instead.
Or the new login controls of version 2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top