Wanted: Example of using ASP.NET inside Azure Function for Role Based Authorization with Azure AD

Siegfried Heintze 1,861 Reputation points
2023-06-02T16:51:48.3233333+00:00

Since I'm implementing an AJAX REST service in a Azure Function for various Azure AD B2C front ends (blazor server, blazor webasm & angular), I believe it is necessary to implement authentication and authorization in the front end and back end azure functions.

To be consistent with role based authorization in ASP.NET razor apps, I'm thinking I would like to use the C# attributes as demonstrated here: https://learn.microsoft.com/en-us/answers/questions/271969/how-to-add-authorization-to-basic-aad-b2c-web-app and specifically in the github example here: https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/5-WebApp-AuthZ/5-1-Roles

Can someone point me to an example implementing Role based authorization & authentication via the C# attributes (or annotations) feature inside an azure function? Can I do it with ASP.NET just like we do with razor apps?

Since I realize that doing authorization in Azure AD B2C is a bit exotic, an Azure AD B2B example might suffice.

Since I am presently grabbing and printing the claims (including my role) from the header in my sample Azure Function, it seems that it would be easy to do a string compare to implement authorization (but that would be clumsy).

Cannot I just use some ASP.NET nuget packages and create some policies for use in C# attributes like Alfredo does in the above link (see Alfredo's reference to his github example)?

Apparently not according to this: https://stackoverflow.com/questions/68225375/how-can-i-use-authorize-attributes-in-httptrigger-of-azure-function and this https://learn.microsoft.com/en-us/azure/azure-functions/security-concepts?tabs=v4. But I see from that stack overflow link that there is a 3rd party implementation: https://github.com/dark-loop/functions-authorize. I will give this a try. I was assuming (and now hoping) to find a solution supported by Microsoft.

This seems very strange to me. Does not everybody need to do authorization inside their azure functions?

Since I'm using Azure API Mgt (APIM) I suppose could create custom policies for different roles (for B2C this would be custom attribute called "role").

Maybe I should consider this approach? perhaps someone could point me to an example of this approach for role based authorization?

Thanks

Siegfried

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,262 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,161 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,465 questions
{count} votes

1 answer

Sort by: Most helpful
  1. navba-MSFT 16,945 Reputation points Microsoft Employee
    2023-06-05T04:13:46.5066667+00:00

    @Siegfried Heintze Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    Firstly, Apologies for the delayed response here!

    I understand you are looking for an example/sample code snippet for using ASP.NET inside Azure Function for Role Based Authorization with Azure AD. To answer your question, it is possible to use AAD RBAC integration with Function App to authorize the request. This can be achieved by using the Authorize attribute to enable authorization for the given function. This also allows to apply authorization for selected functions and can be extended to also incorporate app roles.

            [Authorize]
            [FunctionName("SampleFunction")]
            public static async Task<IActionResult> Run(
                [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "sample")] HttpRequest req,
                ILogger log, ClaimsPrincipal user)
            {.....
            .....
    

    Also note that you have the flexibility to extended and incorporate app roles using [Authorize("admin")].

    More Information:
    Setting up the Function App, Validate Access Token and using Custom Authorize Attributes are all explained here. Another sample code which does the Authentication & Authorization with Azure Active Directory in .NET Azure Function Apps is here.

    Related article:
    https://learn.microsoft.com/en-us/azure/app-service/configure-authentication-provider-aad?tabs=workforce-tenant

    Hope this helps.


    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.