Example #1
0
        /// <summary>
        /// Verifica Token de autenticação (nsc:1:)
        /// </summary>
        /// <param name="token">string do token nsc:1:</param>
        /// <param name="tokenGuid">saída contendo a GUID do token (Session ID)</param>
        /// <param name="info">saída contendo NSCInfo com dados do usuário</param>
        /// <returns></returns>
        public static bool VerifyToken(string token, out Guid tokenGuid, out NSCInfo info)
        {
            tokenGuid = Guid.Empty;
            info      = null;

            string[] splitToken = token.Split(':');
            try
            {
                // Verifica condições
                // Token começa com nsc
                if (splitToken[0] != "nsc")
                {
                    return(false);
                }

                // Para token versão 1:
                if (splitToken[1] == "1")
                {
                    int nscVersion = 1;
                    //obtem as partes
                    byte[] bToken = Convert.FromBase64String(splitToken[2]);
                    byte[] bInfo  = Convert.FromBase64String(splitToken[3]);
                    byte[] bHmac  = Convert.FromBase64String(splitToken[4]);

                    IEnumerable <byte> bHmacMsg = NSC_BYTES.Concat(BitConverter.GetBytes(nscVersion)).Concat(bToken).Concat(bInfo);

                    //calcula HMAC
                    HMACSHA512 hmac = new HMACSHA512(NimbusConfig.CookieHMACKey);
                    hmac.ComputeHash(bHmacMsg.ToArray());

                    //se HMACs baterem, token é valido
                    if (bHmac.SequenceEqual(hmac.Hash))
                    {
                        //desserializa informações
                        Guid    t = new Guid(bToken);
                        NSCInfo i;
                        using (MemoryStream ms = new MemoryStream())
                        {
                            ms.Write(bInfo, 0, bInfo.Length);
                            ms.Seek(0, SeekOrigin.Begin);
                            i = TypeSerializer.DeserializeFromStream <NSCInfo>(ms);
                        }
                        tokenGuid = t;
                        info      = i;
                        return(true);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex) { return(false); }

            return(false);
        }
Example #2
0
        public virtual object ConvertDbValue(object value, Type type)
        {
            if (value == null || value.GetType() == typeof(DBNull))
            {
                return(null);
            }

            if (value.GetType() == type)
            {
                if (type == typeof(byte[]))
                {
                    return(TypeSerializer.DeserializeFromStream <byte[]>(new MemoryStream((byte[])value)));
                }

                return(value);
            }

            if (type.IsValueType)
            {
                if (type == typeof(float))
                {
                    return(value is double?(float)((double)value) : (float)value);
                }

                if (type == typeof(double))
                {
                    return(value is float?(double)((float)value) : (double)value);
                }

                if (type == typeof(decimal))
                {
                    return((decimal)value);
                }
            }

            if (type == typeof(string))
            {
                return(value);
            }

            try
            {
                var convertedValue = TypeSerializer.DeserializeFromString(value.ToString(), type);
                return(convertedValue);
            }
            catch (Exception)
            {
                log.ErrorFormat("Error ConvertDbValue trying to convert {0} into {1}",
                                value, type.Name);
                throw;
            }
        }
        public sealed override object DeepCopyObject(object source)
        {
            if (source == null)
            {
                return(null);
            }

            var type = source.GetType();

            using (var ms = MemoryStreamManager.GetStream())
            {
                TypeSerializer.SerializeToStream(source, type, ms);
                ms.Seek(0, System.IO.SeekOrigin.Begin);
                return(TypeSerializer.DeserializeFromStream(type, ms));
            }
        }
        /// <inheritdoc />
        public sealed override T ReadFromStream <T>(Stream readStream, Encoding effectiveEncoding)
        {
            if (null == readStream)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.readStream);
            }

            // 不是 Stream 都会实现 Position、Length 这两个属性
            //if (readStream.Position == readStream.Length) { return default; }

            try
            {
                return(TypeSerializer.DeserializeFromStream <T>(readStream));
            }
            catch (Exception ex)
            {
                s_logger.LogError(ex.ToString());
                return(default);
        /// <inheritdoc />
        public sealed override object ReadFromStream(Type type, Stream readStream, Encoding effectiveEncoding)
        {
            if (null == readStream)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.readStream);
            }

            // 不是 Stream 都会实现 Position、Length 这两个属性
            //if (readStream.Position == readStream.Length) { return GetDefaultValueForType(type); }

            try
            {
                return(TypeSerializer.DeserializeFromStream(type, readStream));
            }
            catch (Exception ex)
            {
                s_logger.LogError(ex.ToString());
                return(GetDefaultValueForType(type));
            }
        }
Example #6
0
        private bool LoadFromDisk()
        {
            using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (iso.FileExists(Filename))
                {
                    using (var stream = iso.OpenFile(Filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        var deserialized =
                            TypeSerializer.DeserializeFromStream <Dictionary <string, CacheData> >(stream);
                        if (deserialized != null)
                        {
                            Cache = deserialized;
                            return(true);
                        }
                    }
                }
            }

            Cache = new Dictionary <string, CacheData>();
            return(false);
        }
Example #7
0
 public override object ParallelDeserialize(Stream stream)
 {
     return(TypeSerializer.DeserializeFromStream(m_primaryType, stream));
 }
Example #8
0
 public override T DeserializeFromStream <T>(Stream stream)
 {
     return(TypeSerializer.DeserializeFromStream <T>(stream));
 }
Example #9
0
 public override object Deserialize(Stream inputStream)
 {
     inputStream.Seek(0, SeekOrigin.Begin);
     return(TypeSerializer.DeserializeFromStream(_primaryType, inputStream));
 }