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

Commit dfebc20

Browse files
authored
Merge pull request #13 from gareth-james/feature/authorize-filter
Add Web API action metadata also for unauthorized requests fixes #12
2 parents 4f96097 + 88b3575 commit dfebc20

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
1-
using System.Collections.Generic;
2-
using System.Threading;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
35
using System.Threading.Tasks;
46
using System.Web;
57
using System.Web.Http.Controllers;
6-
using System.Web.Http.Filters;
78

89
namespace SerilogWeb.Classic.WebApi
910
{
10-
internal class StoreWebApInfoInHttpContextActionFilter : ActionFilterAttribute
11+
internal static class HttpActionContextExtensions
1112
{
12-
public override void OnActionExecuting(HttpActionContext actionContext)
13-
{
14-
StoreWebApInfoInHttpContext(actionContext);
15-
base.OnActionExecuting(actionContext);
16-
}
17-
18-
public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
19-
{
20-
StoreWebApInfoInHttpContext(actionContext);
21-
return base.OnActionExecutingAsync(actionContext, cancellationToken);
22-
}
23-
24-
25-
private static void StoreWebApInfoInHttpContext(HttpActionContext actionContext)
13+
internal static void StoreWebApInfoInHttpContext(this HttpActionContext actionContext)
2614
{
2715
var currentHttpContext = HttpContext.Current;
2816
if (currentHttpContext == null)
@@ -49,4 +37,4 @@ private static void StoreWebApInfoInHttpContext(HttpActionContext actionContext)
4937
currentHttpContext.Items[Constants.WebApiContextInfoKey] = contextualInfo;
5038
}
5139
}
52-
}
40+
}

src/SerilogWeb.Classic.WebApi/Classic/WebApi/PreApplicationStartModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static void Register()
99
{
1010
GlobalConfiguration.Configuration.Services.Add(typeof(IExceptionLogger),
1111
new WebApiExceptionLogger());
12-
GlobalConfiguration.Configuration.Filters.Add(new StoreWebApInfoInHttpContextActionFilter());
12+
GlobalConfiguration.Configuration.Filters.Add(new StoreWebApInfoInHttpContextAuthenticationFilter());
1313
}
1414
}
1515
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
internal class StoreWebApInfoInHttpContextAuthenticationFilter : IAuthenticationFilter
12+
{
13+
public bool AllowMultiple => false;
14+
15+
public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
16+
{
17+
context.ActionContext.StoreWebApInfoInHttpContext();
18+
return Task.FromResult(0);
19+
}
20+
21+
public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
22+
{
23+
return Task.FromResult(0);
24+
}
25+
}
26+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +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" />
83-
<Compile Include="Classic\WebApi\StoreWebApInfoInHttpContextActionFilter.cs" />
84+
<Compile Include="Classic\WebApi\StoreWebApInfoInHttpContextAuthenticationFilter.cs" />
8485
<Compile Include="Classic\WebApi\WebApiExceptionLogger.cs" />
8586
<Compile Include="Classic\WebApi\WebApiRequestInfoKey.cs" />
8687
<Compile Include="Properties\AssemblyInfo.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+
[Authorize]
24+
public void ShouldBeUnauthorized()
25+
{
26+
}
27+
2128
// GET api/values/5
2229
public string Get(int id)
2330
{

0 commit comments

Comments
 (0)