Esempio n. 1
0
        /// <summary>
        /// 判断Ticket和当前HttpContext中的信息是否吻合,包括ClientIP,身份和时间差
        /// </summary>
        /// <returns></returns>
        public bool IsValid(out string reason)
        {
            bool result = true;

            DateTime now = DateTime.UtcNow;

            TimeSpan tSpan = now - this.ClickTime;

            tSpan = TimeSpan.FromTicks(Math.Abs(tSpan.Ticks));

            string currentClientIP = EnvironmentHelper.GetClientIP();
            string currenctUserID  = DeluxePrincipal.IsAuthenticated ? DeluxeIdentity.CurrentUser.ID : this.UserID;

            try
            {
                (string.Compare(currentClientIP, this.ClientIP, true) == 0).FalseThrow <InvalidOperationException>(
                    "Ticket中的ClientIP为{0},当前请求的ClientIP为{1},不匹配", this.ClientIP, currentClientIP);

                (string.Compare(currenctUserID, this.UserID, true) == 0).FalseThrow <InvalidOperationException>(
                    "Ticket中的UserID为{0},当前请求的UserID为{1},不匹配", this.UserID, currenctUserID);

                (tSpan.CompareTo(RelativeTicketSettings.GetConfig().UrlTransferTimeout) <= 0).FalseThrow <InvalidOperationException>(
                    "Ticket中的ClickTime为{0},与服务器当前时间{1}差别太大", this.ClickTime, now);

                reason = string.Empty;
            }
            catch (InvalidOperationException ex)
            {
                reason = ex.Message;
                result = false;
            }

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 得到一个加密的串
        /// </summary>
        /// <returns></returns>
        public string EncryptToString()
        {
            string data = JSONSerializerExecute.Serialize(this);

            byte[] encData = RelativeTicketSettings.GetConfig().Encryptor.EncryptString(data);

            return(Convert.ToBase64String(encData));
        }
Esempio n. 3
0
        public static RelativeTicket DecryptFromString(string ticketString)
        {
            ExceptionHelper.CheckStringIsNullOrEmpty(ticketString, "ticketString");

            byte[]         data    = Convert.FromBase64String(ticketString);
            string         decData = RelativeTicketSettings.GetConfig().Encryptor.DecryptString(data);
            RelativeTicket ticket  = (RelativeTicket)JSONSerializerExecute.DeserializeObject(decData, typeof(RelativeTicket));

            return(ticket);
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            bool result = false;

            RelativeTicket objParam = (RelativeTicket)obj;

            TimeSpan tSpan = objParam.ClickTime - this.ClickTime;

            if (objParam.ClientIP == this.ClientIP && objParam.UserID == this.UserID &&
                tSpan.CompareTo(RelativeTicketSettings.GetConfig().UrlTransferTimeout) <= 0)
            {
                result = true;
            }

            return(result);
        }