简体   繁体   中英

How can I take from Session byte's array? Inside the session was written byte's array too

Is it byte inside the session or data will be converted into the string after writing? if yes I think I can take it like this:

var res = Encoding.UTF8.GetBytes(Session["session_state"]);

or can I take it "as is" without converting into the byte array? like:

var res = Session["session_state"] as bytes[]; // or smth. like that

The data isn't converted. If the session object is serialized (depending on how it is stored), then it is deserialized before you get access to it again.

Just cast the value to a byte array:

var res = Session["session_state"] as byte[];

or:

var res = (byte[])Session["session_state"];

Side note: A byte array can't reliably be converted to a string using UTF-8 encoding. UTF-8 is used the other way around, ie converting a string to bytes and then back. To make a string from bytes you would rather use something like base64.

You will always get what you stored in the session regardless of the mode you are using for the session state (inproc, state server, ...)

So the answer will be

var res = Session["session_state"] as byte[];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM