Public client applications, commonly used in desktop or mobile contexts, do not store secrets. In contrast, confidential client applications (such as web APIs or backend servers) require a client secret and a redirect URI for secure authentication callbacks.
Advanced Client Application Customizations
After initializing the client applications, you can customize their behavior using several important methods provided by the MSAL.NET library:- Authority: Configure the application authority to Microsoft Entra or a custom endpoint, determining the cloud or tenant used for authentication.
- WithTenantId: Specifically sets the tenant ID, which is particularly useful for multi-tenant applications.
- WithClientId: Allows reusing the configuration across different clients by overriding the default client ID.
- WithRedirectUri: Defines the URI to which users are redirected after authentication, crucial for public client applications.
- WithComponent: Tags the library or component name used with MSAL.NET to assist in telemetry.
- WithDebugLoggingCallback: Enables custom debug logging to support troubleshooting during the authentication process.
- WithLogging: Provides general logging capabilities to track application behavior during authentication.
- WithTelemetry: Sends telemetry data for diagnostic purposes, helping to analyze and improve authentication performance over time.
Building a Confidential Client Application in Visual Studio Code
In this section, we will build a confidential client application using Visual Studio Code. The process involves using the ConfidentialClientApplicationBuilder and providing essential details such as the client ID, client secret, required scopes, and authority URI built with your tenant ID. Below is a typical setup:Creating a Service Principal Using Azure CLI
You can create a service principal directly from the terminal with the Azure CLI command:Ensure you securely copy and store these credentials. The password will not be displayed again, so avoid hard coding these values in your source code. Utilize a configuration file with proper security measures for production environments.
Testing the Application
Once you update the tenant ID, client ID, and client secret in your code, execute the application. An access token will be generated and printed to the console. To verify the token:- Copy the access token.
- Paste it into jwt.ms.