public static List<WebApiUserCacheData> Data()
		{
			var data = HttpRuntime.Cache[Key] as List<WebApiUserCacheData>;
			if (data == null)
			{
				lock (_lock)
				{
					data = HttpRuntime.Cache[Key] as List<WebApiUserCacheData>;

					if (data == null)
					{
						var engine = EngineContext.Current;
						var genericAttributes = engine.Resolve<IRepository<GenericAttribute>>();
						var customers = engine.Resolve<IRepository<Customer>>();

						var attributes = (
							from a in genericAttributes.Table
							join c in customers.Table on a.EntityId equals c.Id
							where !c.Deleted && c.Active && a.KeyGroup == "Customer" && a.Key == Key
							select new
							{
								a.Id,
								a.EntityId,
								a.Value
							}).ToList();

						data = new List<WebApiUserCacheData>();

						foreach (var attribute in attributes)
						{
							if (!string.IsNullOrWhiteSpace(attribute.Value) && !data.Exists(x => x.CustomerId == attribute.EntityId))
							{
								string[] arr = attribute.Value.SplitSafe("¶");

								if (arr.Length > 2)
								{
									var apiUser = new WebApiUserCacheData
									{
										GenericAttributeId = attribute.Id,
										CustomerId = attribute.EntityId,
										Enabled = bool.Parse(arr[0]),
										PublicKey = arr[1],
										SecretKey = arr[2]
									};

									if (arr.Length > 3)
										apiUser.LastRequest = DateTime.ParseExact(arr[3], "o", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

									if (apiUser.IsValid)
										data.Add(apiUser);
								}
							}
						}

						HttpRuntime.Cache.Add(Key, data, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable,
							new CacheItemRemovedCallback(OnDataRemoved));
					}
				}
			}
			return data;
		}
Пример #2
0
        public static List <WebApiUserCacheData> Data()
        {
            var data = HttpRuntime.Cache[Key] as List <WebApiUserCacheData>;

            if (data == null)
            {
                lock (_lock)
                {
                    data = HttpRuntime.Cache[Key] as List <WebApiUserCacheData>;

                    if (data == null)
                    {
                        var engine            = EngineContext.Current;
                        var genericAttributes = engine.Resolve <IRepository <GenericAttribute> >();
                        var customers         = engine.Resolve <IRepository <Customer> >();

                        var attributes = (
                            from a in genericAttributes.Table
                            join c in customers.Table on a.EntityId equals c.Id
                            where !c.Deleted && c.Active && a.KeyGroup == "Customer" && a.Key == Key
                            select new
                        {
                            a.Id,
                            a.EntityId,
                            a.Value
                        }).ToList();

                        data = new List <WebApiUserCacheData>();

                        foreach (var attribute in attributes)
                        {
                            if (!string.IsNullOrWhiteSpace(attribute.Value) && !data.Exists(x => x.CustomerId == attribute.EntityId))
                            {
                                string[] arr = attribute.Value.SplitSafe("¶");

                                if (arr.Length > 2)
                                {
                                    var apiUser = new WebApiUserCacheData
                                    {
                                        GenericAttributeId = attribute.Id,
                                        CustomerId         = attribute.EntityId,
                                        Enabled            = bool.Parse(arr[0]),
                                        PublicKey          = arr[1],
                                        SecretKey          = arr[2]
                                    };

                                    if (arr.Length > 3)
                                    {
                                        apiUser.LastRequest = DateTime.ParseExact(arr[3], "o", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
                                    }

                                    if (apiUser.IsValid)
                                    {
                                        data.Add(apiUser);
                                    }
                                }
                            }
                        }

                        HttpRuntime.Cache.Add(Key, data, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable,
                                              new CacheItemRemovedCallback(OnDataRemoved));
                    }
                }
            }
            return(data);
        }
		public bool CreateKeys(int customerId)
		{
			if (customerId != 0)
			{
				var hmac = new HmacAuthentication();
				var userData = WebApiCachingUserData.Data();
				string key1, key2;

				for (int i = 0; i < 9999; ++i)
				{
					if (hmac.CreateKeys(out key1, out key2) && !userData.Exists(x => x.PublicKey.IsCaseInsensitiveEqual(key1)))
					{
						var apiUser = new WebApiUserCacheData
						{
							CustomerId = customerId,
							PublicKey = key1,
							SecretKey = key2,
							Enabled = true
						};

						RemoveKeys(customerId);

						var attribute = new GenericAttribute
						{
							EntityId = customerId,
							KeyGroup = "Customer",
							Key = WebApiCachingUserData.Key,
							Value = apiUser.ToString()
						};

						_genericAttributeService.InsertAttribute(attribute);

						WebApiCachingUserData.Remove();
						return true;
					}
				}
			}
			return false;
		}