how to maintain session in asp 3.5 with c#

There are different ways. You can use form authentication , it's a way to do that.

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="Login.aspx"
           protection="All"
           timeout="30"
           name=".ASPXAUTH" 
           path="/"
           requireSSL="false"
           slidingExpiration="true"
           defaultUrl="default.aspx"
           cookieless="UseDeviceProfile"
           enableCrossAppRedirects="false" />
  </authentication>

<authorization>  
    <allow users="admin" />
    <deny users="*" />
</authorization>
</system.web>
private void btnLogin_Click(object sender, System.EventArgs e)
{
 if (Validate(txtUsername.Text, txtPassword.Text))
 {
  FormsAuthentication.Initialize();
  String strRole = txtUsername.Text;  
  FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, 
   txtUsername.Text, DateTime.Now, 
   DateTime.Now.AddMinutes(30), false, strRole, 
   FormsAuthentication.FormsCookiePath);
  Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, 
   FormsAuthentication.Encrypt(fat)));
  Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, false));
 }
 else
  lblError.Visible = true;
}

private Boolean Validate(String strUsername, String strPassword)
{
    return ((strUsername == "admin") && (strPassword == "password"));
}
 


In logout page you can use the following code

private void Page_Load(object sender, System.EventArgs e)
{
 Session.Abandon();
 FormsAuthentication.SignOut();
}


remeber this kind of authentication works only user enables cookies

Comments

Popular posts from this blog

AGRICULTURAL SCIENTIST RECRUITMENT BOARD

CIVIL SERVICES' (I.A.S.) EXAMINATION

HSC Exam Time Table 2013 Arts, Commerce, Science