开发者

Cookies and session in asp.net

I am creating a login and the storing the user details in a cookie using this code

if (ValidateUser(txtUserName.Value,txtUserPass.Value) )
{
   //string useremail = Convert.ToString(txtUserName.Value);
   Session.Add("useremail", txtUserName.Value开发者_运维技巧);
   FormsAuthenticationTicket tkt;
   string cookiestr;
   HttpCookie ck;
   tkt = new FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now, 
     DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "your custom data");
   cookiestr = FormsAuthentication.Encrypt(tkt);
   ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
   if (chkPersistCookie.Checked)
     ck.Expires=tkt.Expiration; 
   ck.Path = FormsAuthentication.FormsCookiePath; 
   Response.Cookies.Add(ck);
}

I am also creating a session Session.Add("useremail", txtUserName.Value); After succesfull authentication it is redirected to user.aspx I want to read the useremail value in the user.aspx page but when I tried to access the value in the user page it is not showing useremail field.

protected void Page_Load(object sender, EventArgs e)
    {
        if
            (Session["useremail"] == null) Response.Redirect("Home.aspx");
        else

            BindGridView(useremail);
    }

And this is my webconfig:

<authentication mode="Forms"><forms name=".YAFNET_Authentication" loginUrl="Home.aspx" protection="All" timeout="43200" cookieless="UseCookies"/></authentication>

Correct me if i am doing any wrong. And also please tell me how to pass the useremail value to the user.aspx page so that I can pass that value to gridview function


Just change it to

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["useremail"] == null)
            Response.Redirect("Home.aspx");
        else
            BindGridView((string)Session["useremail"]);
    }


You can add an object to the session state like this:

Session["useremail"] = "john.smith@microsoft.com";

You can then retrieve it in the following manner:

var useremail = Session["useremail"] ?? null;
if (useremail == null)
{
   //...
}
else
{
    BindGridView(useremail);
}

If the item "useremail" is not present in the session state the useremail variable will be set to null otherwhise it will contain the e-mail address.


You are getting confused with relationship between authentication, session state and cookies. In ASP.NET, Session State and Forms Authentication are not linked i.e. their scope are different. You can have some session state for un-authenticated user. Session and forms authentication uses different cookies for tracking purposes and the cookie management is more or less automatic and you don't really need to write code to manage it as you have done. Besides, what you store in the cookie has no bearing on what goes in the session state. Its also possible to have both session and forms authentication to get working w/o cookies. So code such as below should work for session state

Session["key"] = "put your data here";

// retrieve the data elsewhere
var data = Session["key"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜