### Start Server Source: https://github.com/azuread/microsoft-identity-web/blob/master/tests/DevApps/SidecarAdapter/typescript/README.md Use this command to start the development server for the application. ```sh npm run start ``` -------------------------------- ### Setup Azure SDK Integration Source: https://github.com/azuread/microsoft-identity-web/blob/master/docs/calling-downstream-apis/from-web-apps.md Install necessary packages and register the MicrosoftIdentityTokenCredential for Azure service authentication. ```bash dotnet add package Microsoft.Identity.Web.Azure dotnet add package Azure.Storage.Blobs ``` ```csharp using Microsoft.Identity.Web; builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")) .EnableTokenAcquisitionToCallDownstreamApi() .AddInMemoryTokenCaches(); // Add Azure token credential builder.Services.AddMicrosoftIdentityAzureTokenCredential(); ``` -------------------------------- ### Setup Microsoft Graph Integration Source: https://github.com/azuread/microsoft-identity-web/blob/master/docs/calling-downstream-apis/from-web-apps.md Install the required NuGet package and configure the GraphServiceClient in the application startup. ```bash dotnet add package Microsoft.Identity.Web.GraphServiceClient ``` ```csharp // Startup configuration builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")) .EnableTokenAcquisitionToCallDownstreamApi() .AddMicrosoftGraph(options => { options.Scopes = "user.read mail.read"; }) .AddInMemoryTokenCaches(); ``` -------------------------------- ### Verify tool installation Source: https://github.com/azuread/microsoft-identity-web/blob/master/tools/app-provisioning-tool/vs2019-16.9-how-to-use.md Lists installed .NET tools to confirm that msidentity-app-sync is correctly configured. ```bash dotnet tool list -g ``` -------------------------------- ### Install Dependencies Source: https://github.com/azuread/microsoft-identity-web/blob/master/tests/DevApps/SidecarAdapter/typescript/README.md Run this command in your terminal to install all necessary project dependencies. ```sh npm install ``` -------------------------------- ### Install Microsoft.Identity.Web packages Source: https://github.com/azuread/microsoft-identity-web/blob/master/docs/frameworks/msal-dotnet-framework.md Use these commands to install the necessary packages for token cache serialization and certificate management. ```powershell # Token cache serialization Install-Package Microsoft.Identity.Web.TokenCache # Certificate management Install-Package Microsoft.Identity.Web.Certificate ``` ```bash dotnet add package Microsoft.Identity.Web.TokenCache dotnet add package Microsoft.Identity.Web.Certificate ``` -------------------------------- ### Build and install templates from repository Source: https://github.com/azuread/microsoft-identity-web/wiki/Web-API-Template Builds the project templates locally from the cloned repository and installs the resulting NuGet package. ```Shell dotnet pack /p:ClientSemVer=1.8.2 cd ProjectTemplates cd bin cd Debug dotnet new -i Microsoft.Identity.Web.ProjectTemplates.1.4.0.nupkg ``` -------------------------------- ### Daemon Application Example with Azure SDK Source: https://github.com/azuread/microsoft-identity-web/blob/master/docs/calling-downstream-apis/azure-sdks.md This C# example demonstrates setting up a service provider and using MicrosoftIdentityTokenCredential to authenticate with Azure Blob Storage in a daemon application. Ensure you have the necessary Microsoft.Extensions.DependencyInjection, Microsoft.Identity.Abstractions, Microsoft.Identity.Web, and Azure.Storage.Blobs NuGet packages installed. ```csharp using Microsoft.Extensions.DependencyInjection; using Microsoft.Identity.Abstractions; using Microsoft.Identity.Web; using Azure.Storage.Blobs; class Program { static async Task Main(string[] args) { // Build service provider var tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance(); tokenAcquirerFactory.Services.AddMicrosoftIdentityAzureTokenCredential(); var sp = tokenAcquirerFactory.Build(); // Get credential var credential = sp.GetRequiredService(); credential.Options.RequestAppToken = true; // Use with Azure SDK var blobClient = new BlobServiceClient( new Uri("https://myaccount.blob.core.windows.net"), credential); var container = blobClient.GetBlobContainerClient("data"); await foreach (var blob in container.GetBlobsAsync()) { Console.WriteLine($"Blob: {blob.Name}"); } } } ``` -------------------------------- ### Build and Install msidentity-app-sync from Repository Source: https://github.com/azuread/microsoft-identity-web/blob/master/tools/app-provisioning-tool/README.md Installs the msidentity-app-sync tool globally after cloning the repository, building the project, and packing it. This method is useful for development or when using a specific version from the source. ```Shell git clone https://github.com/AzureAD/microsoft-identity-web cd tools\app-provisioning-tool dotnet pack dotnet tool install --global --add-source app-provisioning-tool\nupkg msidentity-app-sync ``` -------------------------------- ### Install Microsoft.Identity.Web and Microsoft.Extensions.Hosting Source: https://github.com/azuread/microsoft-identity-web/blob/master/docs/getting-started/daemon-app.md Use the dotnet CLI to add the necessary packages for Microsoft.Identity.Web and hosting capabilities. ```bash dotnet add package Microsoft.Identity.Web dotnet add package Microsoft.Extensions.Hosting ``` -------------------------------- ### Build and Install msidentity-app-sync from Repository Source: https://github.com/azuread/microsoft-identity-web/blob/master/tools/README.md Builds, packs, and installs the msidentity-app-sync .NET CLI tool locally from the source repository. This is useful for development or testing the tool directly. ```bash cd tools/app-provisioning-tool dotnet build dotnet pack dotnet tool install --global --add-source ./app-provisioning-tool/bin/Debug msidentity-app-sync ``` -------------------------------- ### Install msidentity-app-sync tool Source: https://github.com/azuread/microsoft-identity-web/blob/master/tools/app-provisioning-tool/vs2019-16.9-how-to-use.md Installs the global .NET tool required for automating Azure AD application registration and project configuration. ```bash dotnet tool install -g msidentity-app-sync ``` -------------------------------- ### Build and Install .NET Core Templates from Repository Source: https://github.com/azuread/microsoft-identity-web/wiki/Web-App-Template Builds the project templates from the Microsoft.Identity.Web repository and installs them locally. This involves cloning the repo, packing the project, and then installing the generated NuGet package. ```Shell dotnet pack /p:ClientSemVer=1.15.2 cd ProjectTemplates cd bin cd Debug dotnet new -i Microsoft.Identity.Web.ProjectTemplates.1.15.2.nupkg ``` -------------------------------- ### Install Microsoft.Identity.Web.Azure Package Source: https://github.com/azuread/microsoft-identity-web/blob/master/README-Azure.md Use the dotnet CLI to add the Microsoft.Identity.Web.Azure package to your project. ```sh dotnet add package Microsoft.Identity.Web.Azure ``` -------------------------------- ### Install Microsoft.Identity.Web.OWIN via Package Manager Console Source: https://github.com/azuread/microsoft-identity-web/blob/master/docs/frameworks/owin.md Use this command in the Package Manager Console to install the Microsoft.Identity.Web.OWIN NuGet package. ```powershell Install-Package Microsoft.Identity.Web.OWIN ``` -------------------------------- ### Install .NET Core Templates from NuGet Source: https://github.com/azuread/microsoft-identity-web/wiki/Web-App-Template Installs the Microsoft.Identity.Web.ProjectTemplates NuGet package to make project templates available for use with the `dotnet new` command. ```Shell dotnet new -i Microsoft.Identity.Web.ProjectTemplates::1.15.2 ``` -------------------------------- ### Install msidentity-app-sync CLI Tool Source: https://github.com/azuread/microsoft-identity-web/blob/master/tools/README.md Installs the msidentity-app-sync .NET CLI tool globally from NuGet. This tool automates Azure AD app registration and ASP.NET Core configuration. ```bash dotnet tool install --global msidentity-app-sync ``` -------------------------------- ### Configure Certificate and Secret Fallback Source: https://github.com/azuread/microsoft-identity-web/blob/master/docs/authentication/credentials/client-secrets.md Example configuration for transitioning from client secrets to certificate-based authentication. ```json { "AzureAd": { "ClientCredentials": [ { "SourceType": "KeyVault", "KeyVaultUrl": "https://keyvault.vault.azure.net", "KeyVaultCertificateName": "ProductionCert" }, { "SourceType": "ClientSecret", "ClientSecret": "fallback-secret" } ] } } ``` -------------------------------- ### Configure OWIN Implementation Source: https://github.com/azuread/microsoft-identity-web/blob/master/docs/calling-downstream-apis/custom-apis.md Setup Microsoft Identity Web in an OWIN-based application. ```csharp using Microsoft.Identity.Web; using Microsoft.Identity.Web.OWIN; using Owin; public class Startup { public void Configuration(IAppBuilder app) { OwinTokenAcquirerFactory factory = TokenAcquirerFactory.GetDefaultInstance(); app.AddMicrosoftIdentityWebApp(factory); factory.Services .AddDownstreamApis(factory.Configuration.GetSection("DownstreamAPI")) .AddInMemoryTokenCaches(); factory.Build(); } } ``` -------------------------------- ### Environment-Specific Configuration (Development) Source: https://github.com/azuread/microsoft-identity-web/blob/master/docs/advanced/logging.md Example of appsettings.Development.json to enable Debug logging for Microsoft.Identity and PII logging. ```json { "Logging": { "LogLevel": { "Microsoft.Identity": "Debug" } }, "AzureAd": { "EnablePiiLogging": true } } ``` -------------------------------- ### Install .NET Core Dependencies Source: https://github.com/azuread/microsoft-identity-web/blob/master/tests/DevApps/blazorserver-calls-api/README-Incremental.md Run these commands in the respective project directories to restore .NET Core dependencies. ```console cd Client dotnet restore ``` ```console cd Service dotnet restore ``` -------------------------------- ### Configure Environment and Build Environment Source: https://github.com/azuread/microsoft-identity-web/blob/master/TESTING.md Commands to set up the repository environment and build the solution. ```batch cd C:\ git clone https://github.com/AzureAD/microsoft-identity-web idweb ``` ```batch Set MicrosoftIdentityWebVersion=2.4.0 ``` ```batch dotnet build Microsoft.Identity.Web.sln ``` -------------------------------- ### Create a new Aspire project Source: https://github.com/azuread/microsoft-identity-web/blob/master/docs/frameworks/aspire.md Initializes a new .NET Aspire starter project using the CLI. ```sh aspire new aspire-starter --name MyService ``` -------------------------------- ### Protect Controller Actions with RequiredScope Attribute Source: https://github.com/azuread/microsoft-identity-web/blob/master/docs/authentication/authorization.md Apply the [Authorize] and [RequiredScope] attributes to controllers or actions to enforce specific scopes for access. The example shows how to protect a GET endpoint. ```csharp using Microsoft.AspNetCore.Authorization; using Microsoft.Identity.Web.Resource; [Authorize] [RequiredScope("access_as_user")] public class TodoListController : ControllerBase { [HttpGet] public IActionResult GetTodos() { // Only accessible if token has "access_as_user" scope return Ok(new[] { "Todo 1", "Todo 2" }); } } ``` -------------------------------- ### Get Access Token for App (C#) Source: https://github.com/azuread/microsoft-identity-web/wiki/daemon-scenarios Use this method to obtain an access token for an application's own identity. The scope parameter must be the resource identifier with the '.default' suffix. The optional tenant parameter should be a GUID or domain name for specific tenant access. ```CSharp public async Task ITokenAcquisition.GetAccessTokenForAppAsync(string scope, string? tenant = null) ``` -------------------------------- ### Clone and Run ASP.NET OpenID Connect Sample Source: https://github.com/azuread/microsoft-identity-web/blob/master/docs/frameworks/owin.md Instructions to clone a sample ASP.NET MVC application using Microsoft.Identity.Web.OWIN and run it after updating configuration. ```bash git clone https://github.com/Azure-Samples/ms-identity-aspnet-webapp-openidconnect cd ms-identity-aspnet-webapp-openidconnect # Update Web.config with your Azure AD app registration # Run in Visual Studio ``` -------------------------------- ### Initialize project with dotnet new Source: https://github.com/azuread/microsoft-identity-web/blob/master/tools/app-provisioning-tool/README.md Use this command to create a new web application project template configured for authentication. ```text dotnet new