示例#1
0
        public async Task <IHttpActionResult> Login([FromBody] LoginModel model)
        {
            try
            {
                var message = await AccountService.CheckAccountVilidate(model.UserName, model.Password);

                if (message.ResulType == ValidateType.None)
                {
                    #if DEBUG
                    string header = JsonConvert.SerializeObject(message.Cookie);
#else
                    string header = AesEncryptHelper.EncryptAes(EncryptUtils.Base64Encrypt(JsonConvert.SerializeObject(message.Cookie)));
#endif

                    var result = new TokenModel()
                    {
                        AuthHeader = header, RespMessage = message.ResulType.ToString()
                    };
                    return(Json(result));
                }
                return(BadRequest(message.Message));
            }
            catch (Exception e)
            {
                var errorId = Guid.NewGuid().Str();
                LogHelper.Error(errorId, e);
                return(Json(new { message = $@"发生未知错误,请联系管理员,错误代码:{errorId}" }));
            }
        }
        public void DesEncrypt_Encrypt()
        {
            var source     = "abcefg";
            var ciphertext = AesEncryptHelper.EncryptAes(source);
            var text       = AesEncryptHelper.DecryptAes(ciphertext);

            Assert.AreEqual <string>(source, text);
        }
示例#3
0
        /// <summary>
        /// 添加加密cookie
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strName">名称</param>
        /// <param name="value">需要加密的对象</param>
        /// <param name="expires">过期时间(分钟)</param>
        /// <param name="cryptType">加密方式</param>
        public static void WriteCryptCookie <T>(string strName, T value, int expires = 8 *60, CryptType cryptType = CryptType.Aes)
        {
            string json        = JsonConvert.SerializeObject(value);
            string cryptString = null;

            switch (cryptType)
            {
            case CryptType.Aes:
            {
                cryptString = AesEncryptHelper.EncryptAes(json);

                break;
            }

            case CryptType.Des:
            {
                cryptString = DesEncryptHelper.EncryptDes(json);

                break;
            }

            case CryptType.Sha1:
            {
                cryptString = UnDecryptableEncryptHelpers.Sha1Encrypt(json);
                break;
            }

            case CryptType.Md5:
            {
                cryptString = UnDecryptableEncryptHelpers.Md5Encrypt(json);
                break;
            }

            default:
            {
                cryptString = EncryptUtils.Base64Encrypt(json);
                break;
            }
            }
            #if DEBUG
            cryptString = json;
            #endif
            WriteCookie(strName, cryptString, expires);
        }