Microsoft Azure Services AppAuthentication for Developers is a game-changer for building secure and scalable applications. It provides a seamless way to authenticate and authorize users, APIs, and services.
With Azure Active Directory (AAD), developers can easily integrate authentication into their applications, eliminating the need for custom authentication code. This streamlines the development process and reduces the risk of security vulnerabilities.
AAD offers a robust set of features, including multi-factor authentication, conditional access, and identity protection. These features help protect user identities and prevent unauthorized access to sensitive data.
By leveraging Azure Services AppAuthentication, developers can focus on building innovative applications, rather than spending time on authentication and authorization.
Benefits and Features
Using the built-in authentication in Microsoft Azure Services can save you a significant amount of time and effort.
You can integrate a variety of auth capabilities into your web app or API without implementing them yourself.
Azure App Service allows you to integrate multiple login providers, such as Microsoft Entra, Facebook, Google, and X, into your application.
The built-in authentication feature provides out-of-the-box authentication with federated identity providers, allowing you to focus on the rest of your application.
- Azure App Service integrates multiple login providers, including Microsoft Entra, Facebook, Google, and X.
- It provides out-of-the-box authentication with federated identity providers.
Built-in Benefits
Using the built-in authentication in Azure App Service can save you a lot of time and effort.
You can integrate a variety of auth capabilities into your web app or API without implementing them yourself, which is a huge plus.
This feature is built directly into the platform and doesn't require any particular language, SDK, security expertise, or even any code to utilize.
You can integrate with multiple login providers, such as Microsoft Entra, Facebook, Google, and X.
Here are some of the benefits of using the built-in authentication:
- Integrated auth capabilities
- No need to implement auth yourself
- No language, SDK, or security expertise required
- No code needed
- Integration with multiple login providers
Advantages of Token-Based
Token-based authentication offers several advantages over traditional connection strings. It allows you to establish specific permissions needed by the app on the Azure resource, following the principle of least privilege.
One key benefit is that token-based authentication scopes access to the resource to only the app(s) intended to access it. This means that anyone or any app with a connection string can connect to an Azure resource, but token-based authentication prevents unauthorized access.
A managed identity eliminates the need to store application secrets, making the app more secure. There's no connection string or application secret that can be compromised.
The Azure.Identity package makes using token-based authentication as easy as a connection string. It acquires and manages Microsoft Entra tokens for you, streamlining the authentication process.
By using token-based authentication classes in the Azure Identity library, you can ensure that your app only accesses the resources it needs, reducing the risk of unauthorized access.
Identity and Access
Identity and Access is a crucial aspect of Microsoft Azure Services App Authentication. You can use federated identity, where a third-party identity provider manages user identities and authentication flow for you.
The following identity providers are available by default: Microsoft Entra, Facebook, Google, X, GitHub, Sign in with Apple, and Any OpenID Connect provider. Each provider has a sign-in endpoint, such as ./.auth/login/aad for Microsoft Entra.
To verify limited access to the web app, you need to check the settings in the Microsoft Entra admin center as an Application Developer. You should verify that Supported account types is set to My organization only and that access to your app is limited to users in your organization.
Here is a list of identity providers with their sign-in endpoints:
- Microsoft Entra: ./.auth/login/aad
- Facebook: ./.auth/login/facebook
- Google: ./.auth/login/google
- X: ./.auth/login/x
- GitHub: ./.auth/login/github
- Sign in with Apple: ./.auth/login/apple
- Any OpenID Connect provider: ./.auth/login/<providerName>
Managed Identities
Managed identities are a way to authenticate and authorize access to Azure services without using client secrets or certificates. This is achieved by creating an identity for the application in Azure Active Directory (AAD).
To create a managed identity for a client function app, you can follow the instructions in the Microsoft article mentioned in Example 2. This will help you move away from maintaining client secrets or certificates.
A managed identity can be created using Azure PowerShell or Microsoft 365 CLI. The Microsoft 365 command to add an application permission to the managed identity is: m365 aad approleassignment add — displayName “client-function-app” — resource “server-app-reg-name” — scope “Orders.Read”
Once the permission is assigned, you can create a request to get an access token to access the server app using the managed identity of the client function app.
Here are the steps to grant an access policy to the Key Vault secrets:
1. Review the current content of your Azure Cosmos DB database.
2. Go to the Key vault and select Access configuration.
3. Ensure Vault access policy is selected, and then select Go to access policies.
4. Select + Create.
5. On the Permissions tab, select the Get checkbox for Key permissions and Secret permissions, and then select Next.
6. On the Principal tab, in the search box, enter the name you gave your App Service, select it from the list, and then select Next.
7. On the Review + create tab, select Create.
By following these steps, you can create a Key Vault access policy for your application to see the secrets in that vault.
Configuration and Setup
In Microsoft Azure, you can easily turn on and configure App Service authentication through the Azure portal and app settings, requiring no SDKs, specific languages, or changes to application code.
To enable App Service authentication, simply click on the "Authentication / Authorization" page in your app settings, where you can easily configure your authentication settings.
You can also use the built-in token store provided by App Service, which is a repository of tokens associated with the users of your web apps, APIs, or native mobile apps. This token store is immediately available to your app once you enable authentication with any provider.
Here are some benefits of using the token store:
- Access tokens, ID tokens, and refresh tokens are cached for the authenticated session.
- These tokens are accessible only by the associated user.
- You can retrieve tokens when you need them and tell App Service to refresh them when they become invalid.
Setup
To set up your Azure App Service for authentication and authorization, you'll need to follow these steps. First, configure App Service with a behavior when an incoming request is not authenticated. You can choose to either allow unauthenticated requests or require authentication.
If you allow unauthenticated requests, you'll need to write code to handle authorization decisions. However, if you require authentication, App Service will reject any unauthenticated traffic to your application.
You can also configure the rejection to be an HTTP 302 Found redirect, an HTTP 401 Unauthorized, an HTTP 403 Forbidden, or an HTTP 404 Not found. This will depend on your specific needs and the type of application you're building.
Here are the possible rejection options:
In addition to configuring App Service, you can also enable automatic authentication through the Azure portal and app settings. This will provide built-in authentication and authorization support, allowing you to sign in users with no code in your web app.
Server Environments
In a server environment, each app should be assigned a unique application identity per environment in which the app is run.
For Azure-hosted apps, a Managed Identity service principal is used, which is designed to represent the identity of the app hosted in Azure. This is because Managed identities can only be used with Azure-hosted apps.
If your app is hosted outside of Azure, for example on-premises, an Application service principal is used to represent the identity of the app in Azure. This is created through the application registration process.
To authenticate an Azure-hosted app, you would use the Managed Identity assigned to the app, which would then be used to authenticate the app to other Azure services.
Here's a quick rundown of the authentication methods for server apps:
An app registration is associated with the server function app and is used to secure the server function app. This app registration is the server's identity.
Token Store
The token store is a game-changer for developers who need to work with tokens in their app. It's a built-in repository of tokens associated with users of your web apps, APIs, or native mobile apps.
With the token store, you can easily retrieve tokens when you need them and have App Service refresh them when they become invalid. This saves you from writing code to collect, store, and refresh tokens in your application.
The token store caches ID tokens, access tokens, and refresh tokens for the authenticated session, making them accessible only by the associated user. This ensures that sensitive information remains secure.
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. This will free up resources and simplify your app's configuration.
The token store is immediately available to your app when you enable authentication with any provider.
Authentication Flow
Authentication flow is a crucial aspect of Microsoft Azure Services AppAuthentication. The flow differs depending on whether you're using a provider's SDK or not.
Without a provider SDK, the application delegates federated sign-in to App Service, which is typically the case with browser apps that present the provider's login page to the user. This is also called server-directed flow or server flow.
The server-directed flow involves four steps: sign user in, post-authentication, establish authenticated session, and serve authenticated content. Here's a breakdown of each step:
With a 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, REST APIs, Azure Functions, and JavaScript browser clients.
Security and Protection
Microsoft Azure Services App Authentication offers robust security features to protect your applications from common web attacks.
App Service authentication mitigates CSRF by inspecting client requests for specific conditions.
It looks for POST requests that are authenticated using a session cookie.
The request must also come from a known browser, as determined by the HTTP User-Agent header.
Additionally, the HTTP Origin or HTTP Referer header must be missing or not in the configured list of approved external domains for redirection.
The HTTP Origin header must also be missing or not in the configured list of CORS origins.
This mitigation logic can be worked around by adding your external domain to the redirect list in Authentication settings.
Development and Testing
During local development, your app must authenticate to Azure services, just like it would in production. There are two main strategies for authenticating apps to Azure during local development.
You can create dedicated application service principal objects for use during local development, which allows you to assign specific resource permissions to the service principal objects used by developers. This approach ensures the application only has access to the resources it needs and replicates the permissions it will have in production.
Alternatively, you can authenticate the app to Azure using the developer's credentials during local development, which has the advantage of easier setup but doesn't accurately replicate the permissions the app will run with in production.
Here are the two main strategies for authenticating apps to Azure during local development:
Local Development
Authentication during local development is a crucial aspect of app development. There are two main strategies for authenticating apps to Azure during local development.
The first strategy involves creating dedicated application service principal objects using the App registration process for use during local development. This approach allows you to assign specific resource permissions to the service principal objects used by developers.
This method replicates the permissions the app will have in production, but it requires creating separate service principal objects for each developer. The downside is the extra setup required.
Alternatively, you can authenticate the app to Azure using the developer's credentials during local development. This method has the advantage of easier setup, but it doesn't accurately replicate the permissions the app will run with in production.
In a DEV tenant, you'll need to use the client app registration's ID and secret for authentication during local development. This is because Managed Identity cannot be used for authentication locally.
Here are the two main strategies for authenticating apps to Azure during local development:
.NET Client Library
The .NET client library is a crucial tool for developers working with Azure services. It allows you to access various Azure services such as Cosmos DB, Key Vault, and Azure Identity.
To get started with the .NET client library, you need to import the necessary libraries into your .NET script. You can do this using the dotnet add package command, which adds packages from a pre-configured package feed like NuGet.
Here are the libraries you need to add:
- Microsoft.Azure.Cosmos
- Newtonsoft.Json
- Microsoft.Azure.KeyVault
- Microsoft.Azure.Services.AppAuthentication
You can add these libraries using the following commands:
- dotnet add package Microsoft.Azure.Cosmos --version 3.22.1
- dotnet add package Newtonsoft.Json --version 13.0.1
- dotnet add package Microsoft.Azure.KeyVault
- dotnet add package Microsoft.Azure.Services.AppAuthentication --version 1.6.2
Alternatively, you can use the Azure.Identity library, which is a more modern and recommended approach. With Azure.Identity, you create an instance of ManagedIdentityCredential, which automatically uses the credentials of the managed identity configured for the client function app.
Sources
- https://learn.microsoft.com/en-us/azure/app-service/overview-authentication-authorization
- https://learn.microsoft.com/en-us/azure/app-service/scenario-secure-app-authentication-app-service
- https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication/
- https://microsoftlearning.github.io/dp-420-cosmos-db-dev/instructions/28-key-vault.html
- https://anoopt.medium.com/few-ways-of-obtaining-access-token-in-azure-application-to-application-authentication-40a9473a2dde
Featured Images: pexels.com