Esempio n. 1
0
        private static long GetTimestamp(IClockService clock)
        {
            var offsetNow = clock.GetNowOffset();

            var offsetBaseTime = new DateTimeOffset(ExtensionSettings.Preference.BaseDateTime, offsetNow.Offset);

            return((long)(offsetNow - offsetBaseTime).TotalMilliseconds);
        }
Esempio n. 2
0
        public static ICreation <TCreatedBy> PopulateCreation <TCreatedBy>
            (this ICreation <TCreatedBy> creation, IClockService clock)
            where TCreatedBy : IEquatable <TCreatedBy>
        {
            creation.NotNull(nameof(creation));
            clock.NotNull(nameof(clock));

            creation.CreatedTime      = clock.GetNowOffset();
            creation.CreatedTimeTicks = creation.CreatedTime.Ticks;

            return(creation);
        }
Esempio n. 3
0
        protected virtual byte[] GetCurrentTimestamp(IClockService clock)
        {
            var now = clock.GetNowOffset();

            var buffer = BitConverter.GetBytes(now.Ticks / 10000L);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(buffer);
            }

            return(buffer);
        }
Esempio n. 4
0
        public static object GetDateTimeNow(IClockService clock, Type dateTimeType)
        {
            clock.NotNull(nameof(clock));
            dateTimeType.NotNull(nameof(dateTimeType));

            if (dateTimeType.IsGenericType &&
                dateTimeType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                dateTimeType = dateTimeType.UnwrapNullableType();
            }

            if (dateTimeType == typeof(DateTime))
            {
                return(clock.GetNow());
            }

            else if (dateTimeType == typeof(DateTimeOffset))
            {
                return(clock.GetNowOffset());
            }

            return(null);
        }
Esempio n. 5
0
        private List <SecurityTokenKeyInfo> GetKeyInfos()
        {
            return(ExtensionSettings.Preference.RunLocker(() =>
            {
                return _memoryCache.GetOrCreate(GetCacheKey(), entry =>
                {
                    if (_keysFilePath.Exists())
                    {
                        return _keysFilePath.ReadSecureJson <List <SecurityTokenKeyInfo> >();
                    }

                    var infos = GenerateKeyInfos(CoreSettings.Preference.SecurityTokensCount);
                    _keysFilePath.WriteSecureJson(infos);
                    return infos;
                });
            }));

            // GenerateKeyInfos
            List <SecurityTokenKeyInfo> GenerateKeyInfos(int count)
            {
                var infos = new List <SecurityTokenKeyInfo>();

                while (infos.Count < count)
                {
                    var createdTime = _clock.GetNowOffset();
                    var token       = SecurityToken.New();

                    var info = SecurityTokenKeyInfo.Create(token, createdTime);
                    if (!infos.Any(p => p.Index == info.Index))
                    {
                        infos.Add(info);
                    }
                }

                return(infos);
            }
        }