Reputation: 3
On my site i did "remember me" once login on my admin account, and than i did logout(It deletes cookie once you logout) and i wanted to enter with another account and checking " remember me", and i get this error:
Unable to cast object of type 'System.Web.HttpCookie' to type 'System.String'.
I think it input some weird thing on my Session but i have no idea why..
What i have tried already: I have tried already everything, and i have no idea why it doesn't work.
Anyone know how to fix? Thanks.
here code:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["account"] != null)
{
Session["user2"] = Request.Cookies["userFont"];
Session["user"] = Request.Cookies["account"];
}
if (Session["user"] == null)
{
Session["user2"] = "Guest";
hey = (string)Session["user2"];
log += "You are not logged in, <a href='Login.aspx'><font color='red'>Log in!</font></a> or <a href='Register.aspx'><font color='red'>Register!</font></a> ";
button += "<td><a href='Login.aspx' class='button'>Log in</a></td>";
}
else
{
hi = (string)Session["user"];
hey = "<a href='\\Profile.aspx?user="+hi+"'>" + Session["user2"] + "</a>";
button += "<td><a href='Login.aspx' class='button'>Log out</a></td>";
}
i am getting error on:
hi = (string)Session["user"];
Upvotes: 0
Views: 1297
Reputation: 6248
This here Session["user"] = Request.Cookies["account"];
sets a cookie not a string.
Use ((HttpCookie)Session["user"]).Value;
to get your string back from the Cookie.
http://msdn.microsoft.com/en-us/library/system.web.httpcookie.aspx
Upvotes: 1