This repository was archived by the owner on Oct 2, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +43
-21
lines changed
src/SerilogWeb.Classic.WebApi
test/SerilogWeb.Classic.WebApi.Test/Controllers Expand file tree Collapse file tree 5 files changed +43
-21
lines changed Original file line number Diff line number Diff line change 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 ;
35using System . Threading . Tasks ;
46using System . Web ;
57using System . Web . Http . Controllers ;
6- using System . Web . Http . Filters ;
78
89namespace 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+ }
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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" />
Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments