Skip to content

Latest commit

 

History

History
97 lines (66 loc) · 6.11 KB

quickstart-web-app-dotnet-core-sign-in.md

File metadata and controls

97 lines (66 loc) · 6.11 KB
title description author manager ms.author ms.custom ms.date ms.reviewer ms.service ms.topic
Quickstart: Sign in to a web app & call an API - ASP.NET Core
Learn how an ASP.NET Core web app uses Microsoft.Identity.Web to implement Microsoft sign-in using OpenID Connect and call Microsoft Graph
cilwerner
CelesteDG
cwerner
devx-track-csharp, scenarios:getting-started, languages:aspnet-core
01/18/2024
jmprieur
identity-platform
quickstart

Quickstart: Sign in users and call the Microsoft Graph API from an ASP.NET Core web app

This quickstart uses a sample ASP.NET Core web app to show you how to sign in users by using the authorization code flow and call the Microsoft Graph API. The sample uses Microsoft Authentication Library for .NET and Microsoft Identity Web to handle authentication.

Prerequisites

Register the application and record identifiers

[!INCLUDE Register a single-page application]

Add a platform redirect URI

[!INCLUDE Add a platform redirect URI]

Clone or download the sample application

To obtain the sample application, you can either clone it from GitHub or download it as a .zip file.

  • To clone the sample, open a command prompt and navigate to where you wish to create the project, and enter the following command:

    git clone https://github.com/Azure-Samples/ms-identity-docs-code-dotnet.git
  • Download the .zip file. Extract it to a file path where the length of the name is fewer than 260 characters.

Create and upload a self-signed certificate

  1. Using your terminal, use the following commands to navigate to create a self-signed certificate in the project directory.

    cd ms-identity-docs-code-dotnet\web-app-aspnet\
    dotnet dev-certs https -ep ./certificate.crt --trust
  2. Return to the Microsoft Entra admin center, and under Manage, select Certificates & secrets > Upload certificate.

  3. Select the Certificates (0) tab, then select Upload certificate.

  4. An Upload certificate pane appears. Use the icon to navigate to the certificate file you created in the previous step, and select Open.

  5. Enter a description for the certificate, for example Certificate for aspnet-web-app, and select Add.

  6. Record the Thumbprint value for use in the next step.

Configure the project

  1. In your IDE, open the project folder, ms-identity-docs-code-dotnet\web-app-aspnet, containing the sample.

  2. Open appsettings.json and replace the file contents with the following snippet;

    :::code language="json" source="~/../ms-identity-docs-code-dotnet/web-app-aspnet/appsettings.json" :::

    • TenantId - The identifier of the tenant where the application is registered. Replace the text in quotes with the Directory (tenant) ID that was recorded earlier from the overview page of the registered application.
    • ClientId - The identifier of the application, also referred to as the client. Replace the text in quotes with the Application (client) ID value that was recorded earlier from the overview page of the registered application.
    • ClientCertificates - A self-signed certificate is used for authentication in the application. Replace the text of the CertificateThumbprint with the thumbprint of the certificate that was previously recorded.

Run the application and sign in

  1. In your project directory, use the terminal to enter the following command;

    dotnet run
  2. Copy the https URL that appears in the terminal, for example, https://localhost:5001, and paste it into a browser. We recommend using a private or incognito browser session.

  3. Follow the steps and enter the necessary details to sign in with your Microsoft account. You're requested to provide an email address so a one time passcode can be sent to you. Enter the code when prompted.

  4. The application requests permission to maintain access to data you have given it access to, and to sign you in and read your profile. Select Accept.

  5. The following screenshot appears, indicating that you have signed in to the application and have accessed your profile details from the Microsoft Graph API.

    :::image type="content" source="./media/common-web-app/dotnet-core/display-api-call-results-dotnet-core.png" alt-text="Screenshot depicting the results of the API call." lightbox="./media/common-web-app/dotnet-core/display-api-call-results-dotnet-core.png":::

Sign-out from the application

  1. Find the Sign out link in the top right corner of the page, and select it.
  2. You're prompted to pick an account to sign out from. Select the account you used to sign in.
  3. A message appears indicating that you signed out. You can now close the browser window.

Related content