March 29, 2024

Authentication and authorization in Azure App Service and Azure Functions

[Copied from – https://docs.microsoft.com/en-us/azure/app-service/overview-authentication-authorization]

Azure App Service provides built-in authentication and authorization support (sometimes referred to as “Easy Auth”), so you can sign in users and access data by writing minimal or no code in your web app, RESTful API, and mobile back end, and also Azure Functions. This article describes how App Service helps simplify authentication and authorization for your app.

Secure authentication and authorization require deep understanding of security, including federation, encryption, JSON web tokens (JWT) management, grant types, and so on. App Service provides these utilities so that you can spend more time and energy on providing business value to your customer.

How it works

On Windows

The authentication and authorization module runs in the same sandbox as your application code. When it’s enabled, every incoming HTTP request passes through it before being handled by your application code.

An architecture diagram showing requests being intercepted by a process in the site sandbox which interacts with identity providers before allowing traffic to the deployed site

This module handles several things for your app:

  • Authenticates users with the specified provider
  • Validates, stores, and refreshes tokens
  • Manages the authenticated session
  • Injects identity information into request headers

The module runs separately from your application code and is configured using app settings. No SDKs, specific languages, or changes to your application code are required.

On Containers

The authentication and authorization module runs in a separate container, isolated from your application code. Using what’s known as the Ambassador pattern, it interacts with the incoming traffic to perform similar functionality as on Windows. Because it does not run in-process, no direct integration with specific language frameworks is possible; however, the relevant information that your app needs is passed through using request headers as explained below.

User/Application claims

For all language frameworks, App Service makes the claims in the incoming token (whether that be from an authenticated end user or a client application) available to your code by injecting them into the request headers. For ASP.NET 4.6 apps, App Service populates ClaimsPrincipal.Current with the authenticated user’s claims, so you can follow the standard .NET code pattern, including the [Authorize] attribute. Similarly, for PHP apps, App Service populates the _SERVER['REMOTE_USER'] variable. For Java apps, the claims are accessible from the Tomcat servlet.

For Azure FunctionsClaimsPrincipal.Current is not populated for .NET code, but you can still find the user claims in the request headers, or get the ClaimsPrincipal object from the request context or even through a binding parameter. See working with client identities for more information.

For more information, see Access user claims.

 Note:

At this time, ASP.NET Core does not currently support populating the current user with the Authentication/Authorization feature. However, some 3rd party, open source middleware components do exist to help fill this gap.

Token store

App Service provides a built-in token store, which is a repository of tokens that are associated with the users of your web apps, APIs, or native mobile apps. When you enable authentication with any provider, this token store is immediately available to your app. If your application code needs to access data from these providers on the user’s behalf, such as:

  • post to the authenticated user’s Facebook timeline
  • read the user’s corporate data using the Microsoft Graph API

You typically must write code to collect, store, and refresh these tokens in your application. With the token store, you just retrieve the tokens when you need them and tell App Service to refresh them when they become invalid.

The ID tokens, access tokens, and refresh tokens are cached for the authenticated session, and they’re accessible only by the associated user.

If you don’t need to work with tokens in your app, you can disable the token store in your app’s Authentication / Authorization page.

Logging and tracing

If you enable application logging, you will see authentication and authorization traces directly in your log files. If you see an authentication error that you didn’t expect, you can conveniently find all the details by looking in your existing application logs. If you enable failed request tracing, you can see exactly what role the authentication and authorization module may have played in a failed request. In the trace logs, look for references to a module named EasyAuthModule_32/64.

Identity providers

App Service uses federated identity, in which a third-party identity provider manages the user identities and authentication flow for you. Five identity providers are available by default:

ProviderSign-in endpoint
Azure Active Directory/.auth/login/aad
Microsoft Account/.auth/login/microsoftaccount
Facebook/.auth/login/facebook
Google/.auth/login/google
Twitter/.auth/login/twitter
Any OpenID Connect provider (preview)/.auth/login/<providerName>

When you enable authentication and authorization with one of these providers, its sign-in endpoint is available for user authentication and for validation of authentication tokens from the provider. You can provide your users with any number of these sign-in options with ease.

legacy extensibility path exists for integrating with other identity providers or a custom auth solution, but this is not recommended. Instead, consider using the OpenID Connect support.

Authentication flow

The authentication flow is the same for all providers, but differs depending on whether you want to sign in with the provider’s SDK:

  • Without provider SDK: The application delegates federated sign-in to App Service. This is typically the case with browser apps, which can present the provider’s login page to the user. The server code manages the sign-in process, so it is also called server-directed flow or server flow. This case applies to browser apps. It also applies to native apps that sign users in using the Mobile Apps client SDK because the SDK opens a web view to sign users in with App Service authentication.
  • With provider SDK: The application signs users in to the provider manually and then submits the authentication token to App Service for validation. This is typically the case with browser-less apps, which can’t present the provider’s sign-in page to the user. The application code manages the sign-in process, so it is also called client-directed flow or client flow. This case applies to REST APIs, Azure Functions, and JavaScript browser clients, as well as browser apps that need more flexibility in the sign-in process. It also applies to native mobile apps that sign users in using the provider’s SDK.

 Note

Calls from a trusted browser app in App Service to another REST API in App Service or Azure Functions can be authenticated using the server-directed flow. For more information, see Customize authentication and authorization in App Service.

The table below shows the steps of the authentication flow.

StepWithout provider SDKWith provider SDK
1. Sign user inRedirects client to /.auth/login/<provider>.Client code signs user in directly with provider’s SDK and receives an authentication token. For information, see the provider’s documentation.
2. Post-authenticationProvider redirects client to /.auth/login/<provider>/callback.Client code posts token from provider to /.auth/login/<provider> for validation.
3. Establish authenticated sessionApp Service adds authenticated cookie to response.App Service returns its own authentication token to client code.
4. Serve authenticated contentClient includes authentication cookie in subsequent requests (automatically handled by browser).Client code presents authentication token in X-ZUMO-AUTH header (automatically handled by Mobile Apps client SDKs).

For client browsers, App Service can automatically direct all unauthenticated users to /.auth/login/<provider>. You can also present users with one or more /.auth/login/<provider> links to sign in to your app using their provider of choice.

Authorization behavior

In the Azure portal, you can configure App Service authorization with a number of behaviors when incoming request is not authenticated.

A screenshot showing the "Action to take when request is not authenticated" dropdown

The following headings describe the options.

Allow Anonymous requests (no action)

This option defers authorization of unauthenticated traffic to your application code. For authenticated requests, App Service also passes along authentication information in the HTTP headers.

This option provides more flexibility in handling anonymous requests. For example, it lets you present multiple sign-in providers to your users. However, you must write code.

Allow only authenticated requests

The option is Log in with <provider>. App Service redirects all anonymous requests to /.auth/login/<provider> for the provider you choose. If the anonymous request comes from a native mobile app, the returned response is an HTTP 401 Unauthorized.

With this option, you don’t need to write any authentication code in your app. Finer authorization, such as role-specific authorization, can be handled by inspecting the user’s claims (see Access user claims).

 Caution

Restricting access in this way applies to all calls to your app, which may not be desirable for apps wanting a publicly available home page, as in many single-page applications.

 Note

By default, any user in your Azure AD tenant can request a token for your application from Azure AD. You can configure the application in Azure AD if you want to restrict access to your app to a defined set of users.

More resources

Provider-specific how-to guides:

Interesting links

Azure App Service EasyAuth and Azure Active Directory Flows –

https://techcommunity.microsoft.com/t5/apps-on-azure/azure-app-service-easyauth-and-azure-active-directory-flows/ba-p/1099890

Advanced usage of authentication and authorization in Azure App Service – https://docs.microsoft.com/en-us/azure/app-service/app-service-authentication-how-to

Client Directed Authentication with Azure Mobile Apps – https://devblogs.microsoft.com/premier-developer/client-directed-authentication-with-azure-mobile-apps/

Getting started on managing users and groups using C# – https://docs.microsoft.com/en-us/samples/azure-samples/aad-dotnet-manage-users-groups-and-roles/getting-started-on-managing-users-and-groups-using-c/https://docs.microsoft.com/en-us/samples/azure-samples/aad-dotnet-manage-users-groups-and-roles/getting-started-on-managing-users-and-groups-using-c/

Getting started on managing users and groups using C# (GitHub) – https://github.com/Azure-Samples/aad-dotnet-manage-users-groups-and-roles/tree/master/