Testing Azure Functions in Portal Returning 401 But When Running API Azure Functions Work Properly?

hampton123 1,175 Reputation points
2023-08-11T21:00:25.37+00:00

Currently I have an Azure Function integrated with APIM that calls on another Azure Function with a managed identity to manipulate a blobs within a Storage Account. The Azure Function in APIM also contains a managed identity because it uses B2C for user authorization. Throughout the entire process of the API, the B2C bearer token is passed. When I try to test the two Azure Functions separately in the Azure portal (passing the Authorization Bearer token), I get "401 Unauthorized" errors for both Functions. However, when I do a complete run of the API, everything works properly. I was wondering why this is happening - although it keeps my API secure I want to know why this is the case.

Please let me know if any other information is required. I used this tutorial as a beginning template.

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,457 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,932 questions
Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,538 questions
Microsoft Security | Microsoft Entra | Microsoft Entra External ID
Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2023-08-14T15:16:05.96+00:00

    Hunter B Thanks for posting your question in Microsoft Q&A. As Dr. S. Gomathi suggested, this depends on the context when 401 was thrown. If you have set authorization level as Function, then Function would expect the key in the query string ("?code=secretkey") as described in the doc along with authorization token or you can change it as anonymous depending on your need. Also, note that the doc talks about IP security and enabling it would restrict the access from the azure portal even with valid function key and token.

    If you don't have IP security enabled and face 401 error even with the function key and auth token, then suggest you using Azure Function Diagnostics to investigate the error. I hope this helps and let me know if any questions. Would be happy to answer.

    Update:
    We reviewed the browser console log and found the error was due to CORS policy and adding origin https://functions.azure.com as allowed origin resolved the issue.

    Error: Access to XMLHttpRequest at 'https://<function-name>.azurewebsites.net/api/<api-name>' from origin 'https://functions.azure.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Dr. S. Gomathi 710 Reputation points MVP
    2023-08-12T06:24:07.0666667+00:00

    Hi Hunter,

    Multiple components and interactions are involved in the scenario you're describing, so there could be multiple causes for the "401 Unauthorized" errors when testing Azure Functions individually. Let's disassemble it:

    Token for Bearer and Authorization:

    When you independently test Azure Functions in the Azure portal, you pass the B2C bearer token. This token is used for user authorization when using managed identities with Azure Functions. However, if you evaluate them separately, you may overlook some crucial configuration or context.

    Managed Identification:

    Each instance of Azure Functions and API Management has a managed identity. Managed identities provide applications with secure credentials to access other Azure services. However, managed identities may not function identically during local testing in the Azure portal as they do when executing the complete API. This may be due to variations in the runtime environment, access permissions, or authentication configuration.

    Relationship with the Storage Account:

    If your functions manipulate objects within a Storage Account, they must have the necessary permissions. To perform these operations, the managed identities must be appropriately configured with the required roles (such as Storage Blob Data Contributor). Ensure that the managed identities associated with your functions have the required Storage Account permissions.

    Management of APIs and Invocation of Functions:

    It is the responsibility of API Management to transmit the bearer token and invoke the underlying Azure Functions. The configuration and behaviour of API Management play a critical role in the propagation and use of the token when invoking functions.

    Token Verification and Distribution:

    The manner in which the bearer token is validated and propagated by API Management to your Azure Functions may result in variations between unit testing and executing the entire API. Some aspects of token validation may depend on the policies of API Management, which may operate differently during separate testing.

    Given the complexity of the setup and the interaction between components, it may be necessary to delve into your configuration, policies, and logs to identify the precise cause of the problem. It is essential to ensure that all components are configured uniformly and that managed identities have the appropriate access permissions to resources.

    To diagnose and determine the precise cause of "401 Unauthorised" errors, you may need to:

    When the "401 Unauthorised" error occurs, examine the Azure Function records for specific error messages and details. Examine the API Management instance's policies and configurations, particularly those pertaining to token validation and propagation. Verify the permissions and duties of the managed identities for the Azure Functions and the Storage Account. During local testing in the Azure portal, consider any differences in the runtime environment and how managed identities are handled. If problems persist, you may need to consult Azure's official documentation, contact their support, or seek assistance from a developer community familiar with Azure and API Management configurations.

    If you find this information useful, kindly accept the response. Feel free to ask if you have any additional queries or require further assistance!

    Regards,

    Dr. Gomathi S

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.