C# Authorization filters for Azure Services?

Viewed 74

Question: Is there a filter for ASP.NET Web API's that can filter requests to only allow calls from other azure services?

An authorize filter like this exists where it can read policies, but if say, this service had to make a call to another azure service for some additional information, I want to be able to make that a protected endpoint to specifically the other azure services it interacts with. What is the best way to go about that?

[HttpGet("HQClient/{clientID}")]
[Authorize(Policy = "read:clients")]
public async Task<ActionResult<HQClient>> GetHQClientByID(Guid clientID)
{
     // Implementation
}
1 Answers

Write a middleware that checks that the request is from azure services and call it from the "Configure" method in the startup class (asp.net core) or in the MVCApplication class method (asp.net MVC)

public void Configure(IApplicationBuilder app)
{
    app.Use(async (context, next) =>
    {
        //write the code that check for azure services then..
    
        //this calls the next delegate/middleware in the pipeline
        await next();
    });

}

You can also write a custom filter and do the logic there

//sample filter
class AuthorizeOnlyAzureServices : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //check for azure services
    }
}

//sample filter usage

public class HomeController : Controller
{
    [AuthorizeOnlyAzureServices]
    public ActionResult Index()
    {
        return View();
    }
}

you can also similarly use an attribute

//sample attribute
public class AuthorizeOnlyAzureServicesAttribute : System.Attribute  
{  
    public AuthorizeOnlyAzureServicesAttribute(HttpRequest request)  
    {  
        //check that the request is from azure service  
    }  
}

//sample usage
[AuthorizeOnlyAzureServices(System.Web.HttpContext.Current.Request)]
public ActionResult Index()
{
    //code goes here
    return View();
}
Related