Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In which format the SessionItemShort(varbinary) is deserialized in ASPStateTempSessions table.

I am trying to deserialize in this format,

C#
public ActionResult Home()
        {
            
            ViewBag.Result = Session["username"];
            SqlConnection con = new SqlConnection("Data Source=WSLK1GF-001;Initial Catalog=ASPState;Integrated Security=True");
            con.Open();
            string val = Session.SessionID;
            string query="select SessionItemShort from ASPStateTempSessions where SessionId LIKE '"+Session.SessionID+"%'";
            SqlCommand cmd=new SqlCommand(query,con);
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            byte[] b1 = new byte[0]; //slot to hold raw data in ms/sql varbinary format
            byte[] b2 = new byte[0];
            b1 = (byte[])dr["SessionItemShort"];
            string s = Server.UrlEncode(Encoding.ASCII.GetString(b1));
            b2 = Encoding.ASCII.GetBytes(s);
            MemoryStream ms = new MemoryStream(b2);
            BinaryFormatter bf = new BinaryFormatter();
            ms.Write(b2, 0, b2.Length);
            ms.Seek(0, SeekOrigin.Begin);
            LoginSession ls = null;
            object obj=null;
            try
            {
                obj = (LoginSession)bf.Deserialize(ms);
            }
            catch(Exception ea)
            {
                Response.Write(ea);
                ms.Close();
            }
            ViewBag.Ses = obj;
            return View();
        }



I got an error like this:

The input stream is not a valid binary format.
The starting contents (in bytes) are: 25-31-34-25-30-30-25-30-30-25-30-30-25-30-31-25-30
Posted
Updated 6-Aug-15 0:50am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900