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

Commit

Permalink
Merge pull request #13 from gareth-james/feature/authorize-filter
Browse files Browse the repository at this point in the history
Add Web API action metadata also for unauthorized requests

fixes #12
  • Loading branch information
tsimbalar authored Mar 17, 2018
2 parents 4f96097 + 88b3575 commit dfebc20
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -49,4 +37,4 @@ private static void StoreWebApInfoInHttpContext(HttpActionContext actionContext)
currentHttpContext.Items[Constants.WebApiContextInfoKey] = contextualInfo;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@
<Compile Include="Classic\WebApi\Enrichers\WebApiControllerNameEnricher.cs" />
<Compile Include="Classic\WebApi\Enrichers\WebApiRouteDataEnricher.cs" />
<Compile Include="Classic\WebApi\Enrichers\WebApiRouteTemplateEnricher.cs" />
<Compile Include="Classic\WebApi\HttpActionContextExtensions.cs" />
<Compile Include="Classic\WebApi\PreApplicationStartModule.cs" />
<Compile Include="Classic\WebApi\StoreWebApInfoInHttpContextActionFilter.cs" />
<Compile Include="Classic\WebApi\StoreWebApInfoInHttpContextAuthenticationFilter.cs" />
<Compile Include="Classic\WebApi\WebApiExceptionLogger.cs" />
<Compile Include="Classic\WebApi\WebApiRequestInfoKey.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit dfebc20

Please sign in to comment.