diff --git a/src/SerilogWeb.Classic.WebApi/Classic/WebApi/StoreWebApInfoInHttpContextActionFilter.cs b/src/SerilogWeb.Classic.WebApi/Classic/WebApi/HttpActionContextExtensions.cs similarity index 61% rename from src/SerilogWeb.Classic.WebApi/Classic/WebApi/StoreWebApInfoInHttpContextActionFilter.cs rename to src/SerilogWeb.Classic.WebApi/Classic/WebApi/HttpActionContextExtensions.cs index b7ec0b7..5239552 100644 --- a/src/SerilogWeb.Classic.WebApi/Classic/WebApi/StoreWebApInfoInHttpContextActionFilter.cs +++ b/src/SerilogWeb.Classic.WebApi/Classic/WebApi/HttpActionContextExtensions.cs @@ -1,28 +1,16 @@ -using System.Collections.Generic; -using System.Threading; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.Http.Controllers; -using System.Web.Http.Filters; namespace SerilogWeb.Classic.WebApi { - internal class StoreWebApInfoInHttpContextActionFilter : ActionFilterAttribute + internal static class HttpActionContextExtensions { - public override void OnActionExecuting(HttpActionContext actionContext) - { - StoreWebApInfoInHttpContext(actionContext); - base.OnActionExecuting(actionContext); - } - - public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken) - { - StoreWebApInfoInHttpContext(actionContext); - return base.OnActionExecutingAsync(actionContext, cancellationToken); - } - - - private static void StoreWebApInfoInHttpContext(HttpActionContext actionContext) + internal static void StoreWebApInfoInHttpContext(this HttpActionContext actionContext) { var currentHttpContext = HttpContext.Current; if (currentHttpContext == null) @@ -49,4 +37,4 @@ private static void StoreWebApInfoInHttpContext(HttpActionContext actionContext) currentHttpContext.Items[Constants.WebApiContextInfoKey] = contextualInfo; } } -} \ No newline at end of file +} diff --git a/src/SerilogWeb.Classic.WebApi/Classic/WebApi/PreApplicationStartModule.cs b/src/SerilogWeb.Classic.WebApi/Classic/WebApi/PreApplicationStartModule.cs index e1f1774..f4c62f2 100644 --- a/src/SerilogWeb.Classic.WebApi/Classic/WebApi/PreApplicationStartModule.cs +++ b/src/SerilogWeb.Classic.WebApi/Classic/WebApi/PreApplicationStartModule.cs @@ -9,7 +9,7 @@ public static void Register() { GlobalConfiguration.Configuration.Services.Add(typeof(IExceptionLogger), new WebApiExceptionLogger()); - GlobalConfiguration.Configuration.Filters.Add(new StoreWebApInfoInHttpContextActionFilter()); + GlobalConfiguration.Configuration.Filters.Add(new StoreWebApInfoInHttpContextAuthenticationFilter()); } } } diff --git a/src/SerilogWeb.Classic.WebApi/Classic/WebApi/StoreWebApInfoInHttpContextAuthenticationFilter.cs b/src/SerilogWeb.Classic.WebApi/Classic/WebApi/StoreWebApInfoInHttpContextAuthenticationFilter.cs new file mode 100644 index 0000000..b63c3cc --- /dev/null +++ b/src/SerilogWeb.Classic.WebApi/Classic/WebApi/StoreWebApInfoInHttpContextAuthenticationFilter.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using System.Web; +using System.Web.Http; +using System.Web.Http.Controllers; +using System.Web.Http.Filters; + +namespace SerilogWeb.Classic.WebApi +{ + internal class StoreWebApInfoInHttpContextAuthenticationFilter : IAuthenticationFilter + { + public bool AllowMultiple => false; + + public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken) + { + context.ActionContext.StoreWebApInfoInHttpContext(); + return Task.FromResult(0); + } + + public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken) + { + return Task.FromResult(0); + } + } +} \ No newline at end of file diff --git a/src/SerilogWeb.Classic.WebApi/SerilogWeb.Classic.WebApi.csproj b/src/SerilogWeb.Classic.WebApi/SerilogWeb.Classic.WebApi.csproj index db43504..8564407 100644 --- a/src/SerilogWeb.Classic.WebApi/SerilogWeb.Classic.WebApi.csproj +++ b/src/SerilogWeb.Classic.WebApi/SerilogWeb.Classic.WebApi.csproj @@ -79,8 +79,9 @@ + - + diff --git a/test/SerilogWeb.Classic.WebApi.Test/Controllers/ValuesController.cs b/test/SerilogWeb.Classic.WebApi.Test/Controllers/ValuesController.cs index ffc65ba..95b73dd 100644 --- a/test/SerilogWeb.Classic.WebApi.Test/Controllers/ValuesController.cs +++ b/test/SerilogWeb.Classic.WebApi.Test/Controllers/ValuesController.cs @@ -18,6 +18,13 @@ public string ShouldThrowException() throw new SyntheticException(nameof(ShouldThrowException)); } + [Route("api/values/shouldbeunauthorized")] + [HttpGet] + [Authorize] + public void ShouldBeUnauthorized() + { + } + // GET api/values/5 public string Get(int id) {