コード例 #1
0
 public void GetNowUnixTimestampTest()
 {
     DateTimeUtility.GetNowUnixTimestamp();
     DateTimeUtility.GetNowUnixTimestamp();
     DateTimeUtility.GetNowUnixTimestamp();
     DateTimeUtility.GetNowUnixTimestamp();
 }
コード例 #2
0
        /// <summary>
        /// 產生JWT
        /// </summary>
        /// <param name="tokenModel">JWT資訊</param>
        /// <returns>JWT</returns>
        public string EncodeJwt(TJwtTokenModel tokenModel)
        {
            var model = JsonDocument.Parse(JsonSerializer.Serialize(tokenModel));

            var builder = GetJwtBuilder();

            builder.AddClaim(ClaimName.IssuedAt, DateTimeUtility.GetNowUnixTimestamp());

            foreach (var kv in model.RootElement.EnumerateObject())
            {
                if (kv.Value.ValueKind == JsonValueKind.Null)
                {
                    continue;
                }
                object value = null;
                switch (kv.Value.ValueKind)
                {
                case JsonValueKind.True:
                case JsonValueKind.False:
                    value = kv.Value.GetBoolean();
                    break;

                case JsonValueKind.Number:
                    value = kv.Value.GetDouble();
                    break;

                case JsonValueKind.String:
                    value = kv.Value.GetString();
                    break;
                }
                builder = builder.AddClaim(kv.Name, value);
            }

            return("Bearer " + builder.Encode());
        }
コード例 #3
0
        /// <summary>
        /// 產生狀態碼
        /// </summary>
        /// <returns>狀態碼</returns>
        public virtual string GenerateStateCode()
        {
            var part1 = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 16);
            var part2 = DateTimeUtility.GetNowUnixTimestamp();

            var head = (part1 + part2).ToUpper();
            var hash = (head + Configuration.AppId + Configuration.AppKey).ToHashString <MD5>();

            return(Convert.ToBase64String(Encoding.UTF8.GetBytes(head + hash)));
        }
コード例 #4
0
        /// <summary>
        /// 驗證狀態碼
        /// </summary>
        /// <param name="stateCode">狀態碼</param>
        /// <returns>是否合法</returns>
        public virtual bool VerifyStateCode(string stateCode)
        {
            stateCode = Encoding.UTF8.GetString(Convert.FromBase64String(stateCode));

            var head = stateCode.Substring(0, stateCode.Length - 32);

            if (DateTimeUtility.GetNowUnixTimestamp() - long.Parse(head.Substring(16)) > 60 * 15)
            {
                return(false);
            }

            var hash = stateCode.Substring(stateCode.Length - 32);

            return((head + Configuration.AppId + Configuration.AppKey).ToHashString <MD5>() == hash);
        }
コード例 #5
0
 public void UnixTimestampNow()
 {
     DateTimeUtility.GetNowUnixTimestamp();
 }