Hi,
When i run this code, user can login.
I have Following Questions (doubts):
1) Without Global.asax file, can we directly declare session variable?
2) the session variable will be automatically stored in the 'ASPStateTempSessions' table?
3)When I open the table 'ASPStateTempSessions' to see session variable, Why there will be no rows?
4)Are session variables automatically 'serialized' before storing into 'ASPStateTempSessions' ? or we need to 'serialize' session in code?
Last Question: 5) Once we log out of the Application, will session variables stored inside SQl server gets deleted automatically?
Kindly suggest me friends..
Please..
Following is my Source code:
--------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
string st = ConfigurationManager.ConnectionStrings["SQLCONN"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void cmdLogin_Click(object sender, EventArgs e)
{
try
{
using(SqlConnection cn=new SqlConnection(st))
{
cn.Open();
string sql="select * from TLoginUsers where Uname='"+Txtusername.Text.Trim()+"' and Upasswd='"+txtPasssword.Text.Trim()+"'";
using (SqlCommand cm = new SqlCommand(sql, cn))
{
using(SqlDataAdapter DA=new SqlDataAdapter(cm))
{
using(DataSet DS=new DataSet())
{
DA.Fill(DS);
if (DS.Tables[0].Rows.Count > 0)
{
string k = DS.Tables[0].Rows[0][1].ToString();
FormsAuthentication.RedirectFromLoginPage(DS.Tables[0].Rows[0][1].ToString(), true);
Session["username"] = DS.Tables[0].Rows[0][1];
Response.Redirect("ValidUserPage.aspx");
}
else
{
lblErrorMsg.Text = "invalid username/password. User authentication failed";
FormsAuthentication.RedirectToLoginPage();
}
}
}
}
}
}
catch (Exception ex)
{
lblErrorMsg.Text = ex.Message;
}
}
}
and below is my Web.config's Code:
-------------------------------------
<configuration>
<connectionStrings>
<add name="SQLCONN" connectionString="Data Source=cub-33\sqlexpress;Initial Catalog=KIRAN;Integrated Security=True"/>
</connectionStrings>
<system.web>
<sessionState mode="SQLServer" timeout="300" sqlConnectionString="server=.;Integrated Security=true" cookieless="false">
</sessionState>
<authentication mode="Forms">
<forms loginUrl="LoginPage.aspx" timeout="120" slidingExpiration="true">
</forms>
</authentication>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
</system.web>
</configuration>