public void WrapperResolvesAuthenticationFilterFromDependencyScope() { var builder = new ContainerBuilder(); builder.Register<ILogger>(c => new Logger()).InstancePerDependency(); var activationCount = 0; builder.Register<IAutofacAuthenticationFilter>(c => new TestAuthenticationFilter(c.Resolve<ILogger>())) .AsWebApiAuthenticationFilterFor<TestController>(c => c.Get()) .InstancePerRequest() .OnActivated(e => activationCount++); var container = builder.Build(); var resolver = new AutofacWebApiDependencyResolver(container); var configuration = new HttpConfiguration { DependencyResolver = resolver }; var requestMessage = new HttpRequestMessage(); requestMessage.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, configuration); var contollerContext = new HttpControllerContext { Request = requestMessage }; var controllerDescriptor = new HttpControllerDescriptor { ControllerType = typeof(TestController) }; var methodInfo = typeof(TestController).GetMethod("Get"); var actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, methodInfo); var actionContext = new HttpActionContext(contollerContext, actionDescriptor); var context = new HttpAuthenticationContext(actionContext, Thread.CurrentPrincipal); var metadata = new FilterMetadata { ControllerType = typeof(TestController), FilterScope = FilterScope.Action, MethodInfo = methodInfo }; var wrapper = new AuthenticationFilterWrapper(metadata); wrapper.OnAuthenticate(context); Assert.That(activationCount, Is.EqualTo(1)); }
AsFilterFor <TFilter, TController>(IRegistrationBuilder <object, IConcreteActivatorData, SingleRegistrationStyle> registration, string metadataKey, Expression <Action <TController> > actionSelector) where TController : IHttpController { if (registration == null) { throw new ArgumentNullException(nameof(registration)); } if (actionSelector == null) { throw new ArgumentNullException(nameof(actionSelector)); } var limitType = registration.ActivatorData.Activator.LimitType; if (!limitType.IsAssignableTo <TFilter>()) { var message = string.Format( CultureInfo.CurrentCulture, RegistrationExtensionsResources.MustBeAssignableToFilterType, limitType.FullName, typeof(TFilter).FullName); throw new ArgumentException(message, nameof(registration)); } var metadata = new FilterMetadata { ControllerType = typeof(TController), FilterScope = FilterScope.Action, MethodInfo = GetMethodInfo(actionSelector) }; return(registration.As <TFilter>().WithMetadata(metadataKey, metadata)); }
/// <summary> /// Initializes a new instance of the <see cref="AuthorizationFilterWrapper"/> class. /// </summary> /// <param name="filterMetadata">The filter metadata.</param> public AuthorizationFilterWrapper(FilterMetadata filterMetadata) { if (filterMetadata == null) { throw new ArgumentNullException(nameof(filterMetadata)); } this._filterMetadata = filterMetadata; }
public ActionFilterWrapper(FilterMetadata filterMetadata) { if (filterMetadata == null) { throw new ArgumentNullException("filterMetadata"); } _filterMetadata = filterMetadata; }
private static void AsOverrideFor <TFilter, TController>(ContainerBuilder builder, string metadataKey) { var metadata = new FilterMetadata { ControllerType = typeof(TController), FilterScope = FilterScope.Controller, MethodInfo = null }; builder.RegisterInstance(new AutofacOverrideFilter(typeof(TFilter))) .As <IOverrideFilter>() .WithMetadata(metadataKey, metadata); }
private static void AsOverrideFor <TFilter, TController>(ContainerBuilder builder, string metadataKey, Expression <Action <TController> > actionSelector) { if (actionSelector == null) { throw new ArgumentNullException(nameof(actionSelector)); } var metadata = new FilterMetadata { ControllerType = typeof(TController), FilterScope = FilterScope.Action, MethodInfo = GetMethodInfo(actionSelector) }; builder.RegisterInstance(new AutofacOverrideFilter(typeof(TFilter))) .As <IOverrideFilter>() .WithMetadata(metadataKey, metadata); }
/// <summary> /// Initializes a new instance of the <see cref="AuthenticationFilterOverrideWrapper"/> class. /// </summary> /// <param name="filterMetadata">The filter metadata.</param> public AuthenticationFilterOverrideWrapper(FilterMetadata filterMetadata) : base(filterMetadata) { }
/// <summary> /// Initializes a new instance of the <see cref="ActionFilterOverrideWrapper"/> class. /// </summary> /// <param name="filterMetadata">The filter metadata.</param> public ActionFilterOverrideWrapper(FilterMetadata filterMetadata) : base(filterMetadata) { }
/// <summary> /// Initializes a new instance of the <see cref="AuthorizationFilterWrapper"/> class. /// </summary> /// <param name="filterMetadata">The filter metadata.</param> public AuthorizationFilterWrapper(FilterMetadata filterMetadata) { if (filterMetadata == null) throw new ArgumentNullException("filterMetadata"); _filterMetadata = filterMetadata; }
static bool FilterMatchesAction(FilterContext filterContext, MethodInfo methodInfo, string metadataKey, FilterMetadata metadata) { return(metadata.ControllerType != null && metadata.ControllerType.IsAssignableFrom(filterContext.ControllerType) && metadata.FilterScope == FilterScope.Action && metadata.MethodInfo.GetBaseDefinition() == methodInfo.GetBaseDefinition() && !MatchingFilterAdded(filterContext.AddedFilters[metadataKey], metadata)); }
static bool FilterMatchesController(FilterContext filterContext, string metadataKey, FilterMetadata metadata) { return(metadata.ControllerType != null && metadata.ControllerType.IsAssignableFrom(filterContext.ControllerType) && metadata.FilterScope == FilterScope.Controller && metadata.MethodInfo == null && !MatchingFilterAdded(filterContext.AddedFilters[metadataKey], metadata)); }
static bool MatchingFilterAdded(IEnumerable <FilterMetadata> filters, FilterMetadata metadata) { return(filters.Any(filter => filter.ControllerType == metadata.ControllerType && filter.FilterScope == metadata.FilterScope && filter.MethodInfo == metadata.MethodInfo)); }
private static bool FilterMatchesAction(FilterContext filterContext, MethodInfo methodInfo, string metadataKey, FilterMetadata metadata) { // Issue #10: Comparing MethodInfo.MethodHandle rather than just MethodInfo equality // because MethodInfo equality fails on a derived controller if the base class method // isn't marked virtual... but MethodHandle correctly compares regardless. return(metadata.ControllerType != null && metadata.ControllerType.IsAssignableFrom(filterContext.ControllerType) && metadata.FilterScope == FilterScope.Action && metadata.MethodInfo.GetBaseDefinition().MethodHandle == methodInfo.GetBaseDefinition().MethodHandle && !MatchingFilterAdded(filterContext.AddedFilters[metadataKey], metadata)); }
/// <summary> /// Initializes a new instance of the <see cref="ExceptionFilterOverrideWrapper"/> class. /// </summary> /// <param name="filterMetadata">The filter metadata.</param> public ExceptionFilterOverrideWrapper(FilterMetadata filterMetadata) : base(filterMetadata) { }