how to maintain session in asp 3.5 with c#

Posted by Suneel161 under Regular Expressions on 2/12/2010 | Views : 12162 | Status : [Member] | Replies : 6
hiii

how to maintain sessions in login page




Responses

Posted by: Nishithraj on: 2/12/2010 [Member] Bronze

Up
0
Down
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.

Otherwise you can go for others... Check out
http://www.dnzone.com/go?565

Mark as answer If you got the answer...


Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 2/12/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Day Suneel161

Nishithraj is right. when a user logs in you can store the username in the Session

Session["Username"] = Username;



and when the User logs out you must clear the Session because the application will allow access to unauthorised users. so you can clear it like this

Session.Abandon();


or set the Session Variable to null as demonstrated by Nishithraj

Thank you for Posting at Dotnetfunda

Vuyiswa Maseko



Thank you for posting at Dotnetfunda
[Administrator]

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/12/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
I think you must read this article :
http://www.codeproject.com/KB/aspnet/ExploringSession.aspx

to know more about session.

www.abhisheksur.com

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sagarp on: 2/12/2010 [Member] Bronze

Up
0
Down
Session["id"] = txtName.Text;

Response.Redirect("WebForm2.aspx");


//
// get the session value.
// This code must be placed in other page(WebForm2.aspx).
if(Session["id"] != null)
Label1.Text = Session["Name"].ToString();


Thanks
SagarP
http://www.emanonsolutions.net
http://emanonsolutions.blogspot.com/

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bubbly on: 2/13/2010 [Member] Bronze

Up
0
Down
Hello,

Check this link

http://kbalertz.com/932474/maintain-session-state.aspx

++
Thanks & Regards,
Deepika

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raja on: 2/13/2010 [Member] Starter

Up
0
Down
You can read following articles

http://www.dotnetfunda.com/articles/article61.aspx

http://www.dotnetfunda.com/articles/article576-explore-state-management-.aspx

Hope this will help.

Thank you.

Regards,
Raja, USA

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response