public void OnActionExecuting(ActionExecutingContext context) { var ip = context.HttpContext.Connection.RemoteIpAddress; if (ipLockRepository.IsLocked(ip)) { logger.Log($"Illegal ip: {ip.ToString()}"); throw new IpLockedException(); } }
public void Setup() { logger = Substitute.For <IMyLogger>(); logger.Log(Arg.Any <string>()); repository = Substitute.For <IIpLockRepository>(); repository.IsLocked(Arg.Any <IPAddress>()).Returns(false); foreach (var address in lockedIps) { repository.IsLocked(Arg.Is(address.MakeIpAddress())).Returns(true); } foreach (var address in unlockedIps) { repository.IsLocked(Arg.Is(address.MakeIpAddress())).Returns(false); } container = new WindsorContainer(); container.Register(Component.For <IMyLogger>().Instance(logger)); container.Register(Component.For <IIpLockRepository>().Instance(repository)); container.Register(Component.For <IMyActionFilter>().ImplementedBy <IpLockFilter>().LifestyleTransient()); filter = container.Resolve <IMyActionFilter>(); var actionContext = new ActionContext( Substitute.For <HttpContext>(), Substitute.For <RouteData>(), Substitute.For <ActionDescriptor>(), Substitute.For <ModelStateDictionary>() ); context = new ActionExecutingContext( actionContext, Substitute.For <IList <IFilterMetadata> >(), Substitute.For <IDictionary <string, object> >(), Substitute.For <ControllerBase>() ); }