Exemplo n.º 1
0
        internal static object Deserialize(string serializedValue, SerializationMode mode, IMachineKey machineKey) {
            if (String.IsNullOrEmpty(serializedValue)) {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "serializedValue");
            }

            MachineKeyProtection protectionMode = GetMachineKeyProtectionMode(mode);

            try {
                // First, need to decrypt / verify data
                byte[] rawBytes = machineKey.Decode(serializedValue, protectionMode);

                // Next, verify magic header
                if (!ArrayContainsMagicHeader(rawBytes)) {
                    throw new SerializationException(MvcResources.MvcSerializer_MagicHeaderCheckFailed);
                }

                // Finally, deserialize the object graph
                using (MemoryStream ms = new MemoryStream(rawBytes, _magicHeader.Length, rawBytes.Length - _magicHeader.Length)) {
                    return DeserializeGraph(ms);
                }
            }
            catch (Exception ex) {
                throw CreateSerializationException(ex);
            }
        }
Exemplo n.º 2
0
        internal static string Serialize(object state, SerializationMode mode, IMachineKey machineKey)
        {
            MachineKeyProtection protectionMode = GetMachineKeyProtectionMode(mode);

            try {
                // First, need to append the magic header and serialize the object graph
                byte[] rawBytes;
                using (MemoryStream ms = new MemoryStream()) {
                    ms.Write(_magicHeader, 0, _magicHeader.Length);
                    SerializeGraph(ms, state);
                    rawBytes = ms.ToArray();
                }

                // Then, encrypt / sign data
                return(machineKey.Encode(rawBytes, protectionMode));
            }
            catch (Exception ex) {
                throw CreateSerializationException(ex);
            }
        }
Exemplo n.º 3
0
        internal static string Serialize(object state, IMachineKey machineKey)
        {
            try
            {
                // First, need to serialize the object graph
                byte[] rawBytes;
                using (MemoryStream ms = new MemoryStream())
                {
                    SerializeGraph(ms, state);
                    rawBytes = ms.ToArray();
                }

                // Then, encrypt / sign data
                return(machineKey.Protect(rawBytes, _machineKeyPurposes));
            }
            catch (Exception ex)
            {
                throw CreateSerializationException(ex);
            }
        }
Exemplo n.º 4
0
        internal static string Serialize(object state, IMachineKey machineKey)
        {
            try
            {
                // First, need to serialize the object graph
                byte[] rawBytes;
                using (MemoryStream ms = new MemoryStream())
                {
                    SerializeGraph(ms, state);
                    rawBytes = ms.ToArray();
                }

                // Then, encrypt / sign data
                return machineKey.Protect(rawBytes, _machineKeyPurposes);
            }
            catch (Exception ex)
            {
                throw CreateSerializationException(ex);
            }
        }
Exemplo n.º 5
0
        internal static object Deserialize(string serializedValue, IMachineKey machineKey)
        {
            if (String.IsNullOrEmpty(serializedValue))
            {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "serializedValue");
            }

            try
            {
                // First, need to decrypt / verify data
                byte[] rawBytes = machineKey.Unprotect(serializedValue, _machineKeyPurposes);

                // Finally, deserialize the object graph
                using (MemoryStream ms = new MemoryStream(rawBytes, 0, rawBytes.Length))
                {
                    return(DeserializeGraph(ms));
                }
            }
            catch (Exception ex)
            {
                throw CreateSerializationException(ex);
            }
        }
Exemplo n.º 6
0
        internal static object Deserialize(string serializedValue, IMachineKey machineKey)
        {
            if (String.IsNullOrEmpty(serializedValue))
            {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "serializedValue");
            }

            try
            {
                // First, need to decrypt / verify data
                byte[] rawBytes = machineKey.Unprotect(serializedValue, _machineKeyPurposes);

                // Finally, deserialize the object graph
                using (MemoryStream ms = new MemoryStream(rawBytes, 0, rawBytes.Length))
                {
                    return DeserializeGraph(ms);
                }
            }
            catch (Exception ex)
            {
                throw CreateSerializationException(ex);
            }
        }
Exemplo n.º 7
0
        internal static object Deserialize(string serializedValue, SerializationMode mode, IMachineKey machineKey)
        {
            if (String.IsNullOrEmpty(serializedValue))
            {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "serializedValue");
            }

            MachineKeyProtection protectionMode = GetMachineKeyProtectionMode(mode);

            try
            {
                // First, need to decrypt / verify data
                byte[] rawBytes = machineKey.Decode(serializedValue, protectionMode);

                // Next, verify magic header
                if (!ArrayContainsMagicHeader(rawBytes))
                {
                    throw new SerializationException(MvcResources.MvcSerializer_MagicHeaderCheckFailed);
                }

                // Finally, deserialize the object graph
                using (MemoryStream ms = new MemoryStream(rawBytes, _magicHeader.Length, rawBytes.Length - _magicHeader.Length))
                {
                    return(DeserializeGraph(ms));
                }
            }
            catch (Exception ex)
            {
                throw CreateSerializationException(ex);
            }
        }
Exemplo n.º 8
0
        internal static string Serialize(object state, SerializationMode mode, IMachineKey machineKey) {
            MachineKeyProtection protectionMode = GetMachineKeyProtectionMode(mode);

            try {
                // First, need to append the magic header and serialize the object graph
                byte[] rawBytes;
                using (MemoryStream ms = new MemoryStream()) {
                    ms.Write(_magicHeader, 0, _magicHeader.Length);
                    SerializeGraph(ms, state);
                    rawBytes = ms.ToArray();
                }

                // Then, encrypt / sign data
                return machineKey.Encode(rawBytes, protectionMode);
            }
            catch (Exception ex) {
                throw CreateSerializationException(ex);
            }
        }