To create a custom API authorization attribute in C# that ignores the AllowAnonymous attribute, you can subclass the AuthorizeAttribute class and override its OnAuthorization method.
Here's an example of how to create a custom API authorization attribute that ignores the AllowAnonymous attribute:
using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http.Controllers; using System.Web.Http.Filters; public class CustomAuthorizeAttribute : AuthorizeAttribute { public override void OnAuthorization(HttpActionContext actionContext) { if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()) { return; } base.OnAuthorization(actionContext); } protected override void HandleUnauthorizedRequest(HttpActionContext actionContext) { if (actionContext.RequestContext.Principal.Identity.IsAuthenticated) { actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden); } else { base.HandleUnauthorizedRequest(actionContext); } } } In this example, the CustomAuthorizeAttribute class is a subclass of the AuthorizeAttribute class, which is the base class for all authorization attributes in Web API.
The OnAuthorization method is overridden to check if the AllowAnonymous attribute is present on the action or controller, and if so, to skip the authorization check.
The HandleUnauthorizedRequest method is overridden to return a 403 Forbidden status code if the user is authenticated but not authorized, rather than redirecting to a login page.
To use this custom authorization attribute, you can simply apply it to your Web API actions or controllers like any other authorization attribute:
[CustomAuthorize(Roles = "Admin")] public IHttpActionResult MyAction() { // ... } In this example, the CustomAuthorize attribute is applied to the MyAction method, and it requires that the user is authenticated and belongs to the "Admin" role in order to access the action. The AllowAnonymous attribute can still be applied to bypass this authorization check if necessary.
Code Implementation:
public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override bool IsAuthorized(HttpActionContext actionContext) { if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()) { return true; // Ignore authorization for actions or controllers with AllowAnonymous attribute } // Your custom authorization logic here return base.IsAuthorized(actionContext); } } Description: Extend AuthorizeAttribute to create a custom attribute that ignores authorization for actions or controllers with AllowAnonymous attribute.
Code Implementation:
public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override bool IsAuthorized(HttpActionContext actionContext) { if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()) { return true; // Ignore authorization for actions or controllers with AllowAnonymous attribute } // Your custom authentication logic here return base.IsAuthorized(actionContext); } } Description: Customize the authorization attribute to perform custom authentication logic, while respecting the AllowAnonymous attribute.
Code Implementation:
public class CustomAuthorizeAttribute : AuthorizeAttribute { public override void OnAuthorization(HttpActionContext actionContext) { if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()) { return; // Ignore authorization for actions or controllers with AllowAnonymous attribute } // Your custom authorization logic here base.OnAuthorization(actionContext); } } Description: Override the OnAuthorization method to customize authorization logic, checking for the AllowAnonymous attribute.
Code Implementation:
public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override bool IsAuthorized(HttpActionContext actionContext) { if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()) { return true; // Ignore authorization for actions or controllers with AllowAnonymous attribute } // Your custom policy-based authorization logic here return base.IsAuthorized(actionContext); } } Description: Implement custom policy-based authorization logic within the IsAuthorized method.
Code Implementation:
public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override bool IsAuthorized(HttpActionContext actionContext) { if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()) { return true; // Ignore authorization for actions or controllers with AllowAnonymous attribute } // Your custom token-based authorization logic here return base.IsAuthorized(actionContext); } } Description: Implement custom token-based authorization logic within the IsAuthorized method.
Code Implementation:
public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override bool IsAuthorized(HttpActionContext actionContext) { if (actionContext.ActionDescriptor.GetCustomAttributes<CustomAllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<CustomAllowAnonymousAttribute>().Any()) { return true; // Ignore authorization for actions or controllers with CustomAllowAnonymous attribute } // Your custom authorization logic here return base.IsAuthorized(actionContext); } } Description: Extend the authorization attribute to check for a custom AllowAnonymous-like attribute.
Code Implementation:
public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override bool IsAuthorized(HttpActionContext actionContext) { if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()) { return true; // Ignore authorization for actions or controllers with AllowAnonymous attribute } // Your custom role-based authorization logic here return base.IsAuthorized(actionContext); } } Description: Implement custom role-based authorization logic within the IsAuthorized method.
Code Implementation:
public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override bool IsAuthorized(HttpActionContext actionContext) { if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()) { return true; // Ignore authorization for actions or controllers with AllowAnonymous attribute } // Your custom claims-based authorization logic here return base.IsAuthorized(actionContext); } } Description: Implement custom claims-based authorization logic within the IsAuthorized method.
Code Implementation:
public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override void HandleUnauthorizedRequest(HttpActionContext actionContext) { if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()) { return; // Ignore authorization for actions or controllers with AllowAnonymous attribute } // Your custom unauthorized request handling logic here base.HandleUnauthorizedRequest(actionContext); } } Description: Override the HandleUnauthorizedRequest method to provide custom handling for unauthorized requests.
Code Implementation:
public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override bool IsAuthorized(HttpActionContext actionContext) { if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()) { return true; // Ignore authorization for actions or controllers with AllowAnonymous attribute } // Check for a custom policy attribute if (actionContext.ActionDescriptor.GetCustomAttributes<CustomPolicyAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<CustomPolicyAttribute>().Any()) { // Your custom policy-based authorization logic here return true; } return base.IsAuthorized(actionContext); } } Description: Extend the authorization attribute to check for a custom policy attribute before performing authorization logic.
logging matplotlib-3d internal numbers payment-request-api react-state material-design hortonworks-data-platform nant api-design