Exemplo n.º 1
0
        public static ChecksumResult ValidateToken <T>(T entity, string token)
        {
            ChecksumResult result = new ChecksumResult();

            if (!string.IsNullOrWhiteSpace(token))
            {
                try
                {
                    ChecksumModel <T> requestModel = new ChecksumModel <T>(entity);

                    string decryptString = Crypton.DecryptByKey(token, privateKey);

                    ChecksumModel <T> checksumDecrypt = NewtonJson.Deserialize <ChecksumModel <T> >(decryptString);

                    if (!checksumDecrypt.IsExpired())
                    {
                        bool valid = requestModel.HashMD5.Equals(checksumDecrypt.HashMD5);
                        if (valid)
                        {
                            result = new ChecksumResult(ChecksumResult.ChecksumErrorCode.Success);
                        }
                    }
                    else
                    {
                        result = new ChecksumResult(ChecksumResult.ChecksumErrorCode.Expired);
                    }
                }
                catch
                {
                    // Logs
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        public static string GenToken <T>(T entity, int?expiredInMinute = null)
        {
            string token = string.Empty;

            if (!expiredInMinute.HasValue)
            {
                expiredInMinute = checksumExpiredInMinute;
            }

            if (entity != null && !entity.Equals(default(T)))
            {
                ChecksumModel <T> checksumModel = new ChecksumModel <T>(entity, expiredInMinute.Value);

                token = Crypton.EncryptByKey(checksumModel.ToString(), privateKey);
            }

            return(token);
        }