Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit afdfcaa

Browse files
committed
Add StoreWebApInfoInHttpContextAuthorizeFilter to add enricher data for unauthorized requests. Fixes #12
1 parent 4f96097 commit afdfcaa

File tree

5 files changed

+70
-30
lines changed

5 files changed

+70
-30
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Web;
7+
using System.Web.Http.Controllers;
8+
9+
namespace SerilogWeb.Classic.WebApi
10+
{
11+
internal static class HttpActionContextExtensions
12+
{
13+
public static void StoreWebApInfoInHttpContext(this HttpActionContext actionContext)
14+
{
15+
var currentHttpContext = HttpContext.Current;
16+
if (currentHttpContext == null)
17+
return;
18+
19+
var actionDescriptor = actionContext.ActionDescriptor;
20+
var routeData = actionContext.RequestContext.RouteData;
21+
22+
var actionName = actionDescriptor.ActionName;
23+
var controllerName = actionDescriptor.ControllerDescriptor.ControllerName;
24+
25+
var routeTemplate = routeData.Route.RouteTemplate;
26+
var routeDataDictionary = new Dictionary<string, object>(routeData.Values);
27+
28+
var contextualInfo =
29+
new Dictionary<WebApiRequestInfoKey, object>
30+
{
31+
[WebApiRequestInfoKey.RouteUrlTemplate] = routeTemplate,
32+
[WebApiRequestInfoKey.RouteData] = routeDataDictionary,
33+
[WebApiRequestInfoKey.ActionName] = actionName,
34+
[WebApiRequestInfoKey.ControllerName] = controllerName
35+
};
36+
37+
currentHttpContext.Items[Constants.WebApiContextInfoKey] = contextualInfo;
38+
}
39+
}
40+
}

src/SerilogWeb.Classic.WebApi/Classic/WebApi/StoreWebApInfoInHttpContextActionFilter.cs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,14 @@ internal class StoreWebApInfoInHttpContextActionFilter : ActionFilterAttribute
1111
{
1212
public override void OnActionExecuting(HttpActionContext actionContext)
1313
{
14-
StoreWebApInfoInHttpContext(actionContext);
14+
actionContext.StoreWebApInfoInHttpContext();
1515
base.OnActionExecuting(actionContext);
1616
}
1717

1818
public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
1919
{
20-
StoreWebApInfoInHttpContext(actionContext);
20+
actionContext.StoreWebApInfoInHttpContext();
2121
return base.OnActionExecutingAsync(actionContext, cancellationToken);
2222
}
23-
24-
25-
private static void StoreWebApInfoInHttpContext(HttpActionContext actionContext)
26-
{
27-
var currentHttpContext = HttpContext.Current;
28-
if (currentHttpContext == null)
29-
return;
30-
31-
var actionDescriptor = actionContext.ActionDescriptor;
32-
var routeData = actionContext.RequestContext.RouteData;
33-
34-
var actionName = actionDescriptor.ActionName;
35-
var controllerName = actionDescriptor.ControllerDescriptor.ControllerName;
36-
37-
var routeTemplate = routeData.Route.RouteTemplate;
38-
var routeDataDictionary = new Dictionary<string, object>(routeData.Values);
39-
40-
var contextualInfo =
41-
new Dictionary<WebApiRequestInfoKey, object>
42-
{
43-
[WebApiRequestInfoKey.RouteUrlTemplate] = routeTemplate,
44-
[WebApiRequestInfoKey.RouteData] = routeDataDictionary,
45-
[WebApiRequestInfoKey.ActionName] = actionName,
46-
[WebApiRequestInfoKey.ControllerName] = controllerName
47-
};
48-
49-
currentHttpContext.Items[Constants.WebApiContextInfoKey] = contextualInfo;
50-
}
5123
}
5224
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections.Generic;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using System.Web;
5+
using System.Web.Http;
6+
using System.Web.Http.Controllers;
7+
using System.Web.Http.Filters;
8+
9+
namespace SerilogWeb.Classic.WebApi
10+
{
11+
public class StoreWebApInfoInHttpContextAuthorizeFilter : AuthorizeAttribute
12+
{
13+
protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
14+
{
15+
actionContext.StoreWebApInfoInHttpContext();
16+
base.HandleUnauthorizedRequest(actionContext);
17+
}
18+
}
19+
}

src/SerilogWeb.Classic.WebApi/SerilogWeb.Classic.WebApi.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@
7979
<Compile Include="Classic\WebApi\Enrichers\WebApiControllerNameEnricher.cs" />
8080
<Compile Include="Classic\WebApi\Enrichers\WebApiRouteDataEnricher.cs" />
8181
<Compile Include="Classic\WebApi\Enrichers\WebApiRouteTemplateEnricher.cs" />
82+
<Compile Include="Classic\WebApi\HttpActionContextExtensions.cs" />
8283
<Compile Include="Classic\WebApi\PreApplicationStartModule.cs" />
84+
<Compile Include="Classic\WebApi\StoreWebApInfoInHttpContextAuthorizeFilter.cs" />
8385
<Compile Include="Classic\WebApi\StoreWebApInfoInHttpContextActionFilter.cs" />
8486
<Compile Include="Classic\WebApi\WebApiExceptionLogger.cs" />
8587
<Compile Include="Classic\WebApi\WebApiRequestInfoKey.cs" />

test/SerilogWeb.Classic.WebApi.Test/Controllers/ValuesController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public string ShouldThrowException()
1818
throw new SyntheticException(nameof(ShouldThrowException));
1919
}
2020

21+
[Route("api/values/shouldbeunauthorized")]
22+
[HttpGet]
23+
[StoreWebApInfoInHttpContextAuthorizeFilter]
24+
public void ShouldBeUnauthorized()
25+
{
26+
}
27+
2128
// GET api/values/5
2229
public string Get(int id)
2330
{

0 commit comments

Comments
 (0)