SessionExtensionsTuke.cs 913 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Microsoft.AspNetCore.Http
{
    public static class SessionExtensionsTuke
    {
        public static object GetObject(this ISession session, string key)
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream stream = new MemoryStream(session.Get(key));
            return bf.Deserialize(stream);
        }

        public static void SetObject(this ISession session, string key, object value)
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            bf.Serialize(stream, value);
            long len = stream.Length;
            byte[] serializedObject = new byte[len];
            Array.Copy(stream.GetBuffer(), serializedObject, len);
            session.Set(key, serializedObject);
        }
    }
}