A set of technologies in .NET for building web applications and web services. Miscellaneous topics that do not fit into specific categories.
Hi Jai Holloway
That is because you use the wrong namespace and wrong implements. You may mix ASP.NET Core and ASP.NET. In ASP.NET Core, You need custom Microsoft.AspNetCore.Authorization.AuthorizeAttribute instead of System.Web.Http.AuthorizeAttribute.
Reference: Custom Authorization attributes
From your code design, I suggest you implement a custom authorize attribute using IAuthorizationFilter in ASP.NET Core:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace
{
public class AuthoriseWorksheet : Attribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationFilterContext context)
{
if (context.HttpContext.Request.Headers.TryGetValue("Authorization", out var authHeaderValues))
{
string token = authHeaderValues.FirstOrDefault();
if (!string.IsNullOrEmpty(token))
{
try
{
if (token != "SOMEVALUE")
{
context.Result = new UnauthorizedResult();
}
}
catch (Exception)
{
context.Result = new UnauthorizedResult();
}
}
else
{
context.Result = new UnauthorizedResult();
}
}
else
{
context.Result = new UnauthorizedResult();
}
}
}
}
Then you can use it like what you did:
[HttpPost]
[Route("/api/WorkSheet/SubmitGross")]
[AuthoriseWorksheet]
public IActionResult SubmitGross([FromBody]WorkSheetGross ws)
{}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Rena