ActionValidator
IsValid
true
false
Request.End()
Cache[url]=hit;
'TODO
public class HitInfo { private int _hits; public HitInfo() {} public int Hits { get { return _hits; } set { _hits = value; } } }
public static class ActionValidator { private const int DURATION = 10; // 10 min period public enum ActionTypeEnum { FirstVisit = 100, // The most expensive one, choose the value wisely. ReVisit = 1000, // Welcome to revisit as many times as user likes Postback = 5000, // Not must of a problem for us AddNewWidget = 100, AddNewPage = 100, } public static bool IsValid( ActionTypeEnum actionType ) { HttpContext context = HttpContext.Current; if( context.Request.Browser.Crawler ) return false; string key = actionType.ToString() + context.Request.UserHostAddress; var hit = (HitInfo)(context.Cache[key] ?? new HitInfo()); if( hit.Hits > (int)actionType ) return false; else hit.Hits ++; if( hit.Hits == 1 ) context.Cache.Add(key, hit, null, DateTime.Now.AddMinutes(DURATION), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); return true; }
protected override void OnInit(EventArgs e) { base.OnInit(e); if(base.IsPostBack) if(!ActionValidator.IsValid(ActionValidator.ActionTypeEnum.Postback)) Response.End(); else if (IsFirstVisit()) if(!ActionValidator.IsValid(ActionValidator.ActionTypeEnum.FirstVisit)) Response.End(); else if(!ActionValidator.IsValid(ActionValidator.ActionTypeEnum.ReVisit)) Response.End(); } private bool IsFirstVisit() { if (Session["IsFirstVisit"] == null) { Session["IsFirstVisit"] = false; return true; } else return false; }
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.