internal static string GetPolicyValue(this HttpContext context, ApiThrottleOptions options, Policy policy, string policyKey) { switch (policy) { case Policy.Ip: return(Common.IpToNum(options.OnIpAddress(context))); case Policy.UserIdentity: return(options.OnUserIdentity(context)); case Policy.Header: return(context.GetHeaderValue(policyKey)); case Policy.Query: return(context.GetQueryValue(policyKey)); case Policy.RequestPath: return(context.GetRequestPath()); case Policy.Cookie: return(context.GetCookieValue(policyKey)); case Policy.Form: return(context.GetFormValue(policyKey)); default: throw new ArgumentException("参数出错", "policy"); } }
public static ApiThrottleOptions UseRedisCacheAndStorage(this ApiThrottleOptions options, Action <RedisOptions> configure) { Action <RedisCacheOptions> opt1 = (opt) => { configure(opt); opt.SameWithStorage = true; }; options.AddExtension(new RedisCacheOptionsExtension(opt1)); Action <RedisStorageOptions> opt2 = (opt) => { configure(opt); opt.SameWithCache = true; }; options.AddExtension(new RedisStorageOptionsExtension(opt2)); return(options); }
public static IServiceCollection AddApiThrottle(this IServiceCollection services, Action <ApiThrottleOptions> options) { services.Configure(options); services.TryAddSingleton <IStorageProvider, RedisStorageProvider>(); services.TryAddSingleton <ICacheProvider, RedisCacheProvider>(); services.TryAddSingleton <IApiThrottleService, ApiThrottleService>(); //Options and extension service var opts = new ApiThrottleOptions(); options(opts); //foreach (var serviceExtension in opts.Extensions) //{ // serviceExtension.AddServices(services); //} services.AddSingleton(opts); return(services); }
public ApiThrottleMiddleware(RequestDelegate next, ICacheProvider cache, IOptions <ApiThrottleOptions> options) { _next = next; _cache = cache; _options = options.Value; }
public static ApiThrottleOptions UseRedisStorage(this ApiThrottleOptions options, Action <RedisStorageOptions> configure) { options.AddExtension(new RedisStorageOptionsExtension(configure)); return(options); }
public ApiThrottleService(ICacheProvider cache, IOptions <ApiThrottleOptions> options, IStorageProvider storage) { _cache = cache; _options = options.Value; _storage = storage; }