Skip to content

Latest commit

 

History

History
107 lines (66 loc) · 5.15 KB

sample-web-app-dotnet-sign-in.md

File metadata and controls

107 lines (66 loc) · 5.15 KB
title description author manager ms.author ms.service ms.subservice ms.topic ms.date ms.custom
Sign in users to a sample ASP.NET Core web application
Learn how to configure a sample ASP.NET Core web app to sign in and sign out users by using an external tenant.
cilwerner
celestedg
cwerner
entra-external-id
customers
sample
04/23/2024
developer, devx-track-dotnet

Sign in users for a sample ASP.NET Core web app in an external tenant

This how-to guide uses a sample ASP.NET Core web application to show the fundamentals of modern authentication using the Microsoft Authentication Library for .NET and Microsoft Identity Web for ASP.NET Core to handle authentication.

In this article, you'll register a web application in the Microsoft Entra admin center and create a sign in and sign out user flow. You'll associate your web application with the user flow, download and update a sample ASP.NET Core web application using your own external tenant details. Finally, you'll run and test the sample web application.

Prerequisites

  • Although any IDE that supports ASP.NET Core applications can be used, Visual Studio Code is used for this guide. It can be downloaded from the Downloads page.
  • .NET 7.0 SDK.
  • An external tenant. If you don't already have one, sign up for a free trial.

Register the web app

[!INCLUDE active-directory-b2c-register-app]

Define the platform and URLs

[!INCLUDE ciam-redirect-url-dotnet]

Enable implicit and hybrid flows

[!INCLUDE ciam-enable-implicit-flow]

Add app client secret

[!INCLUDE ciam-add-client-secret]

Grant API permissions

[!INCLUDE ciam-grant-delegated-permissions]

Create a user flow

[!INCLUDE ciam-app-integration-add-user-flow]

Associate the web application with the user flow

[!INCLUDE ciam-integration-add-user-flow]

Clone or download sample web 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-ciam-dotnet-tutorial.git
  • Download the .zip file. Extract it to a file path where the length of the name is fewer than 260 characters.

Configure the application

  1. Navigate to the root folder of the sample you have downloaded and directory that contains the ASP.NET Core sample app:

    cd 1-Authentication\1-sign-in-aspnet-core-mvc
  2. Open the appsettings.json file.

  3. In Authority, find Enter_the_Tenant_Subdomain_Here and replace it with the subdomain of your tenant. For example, if your tenant primary domain is caseyjensen@onmicrosoft.com, the value you should enter is casyjensen.

  4. Find the Enter_the_Application_Id_Here value and replace it with the application ID (clientId) of the app you registered in the Microsoft Entra admin center.

  5. Replace Enter_the_Client_Secret_Here with the client secret value you set up in Add app client secret.

Run the code sample

  1. From your shell or command line, execute the following commands:

    dotnet run
  2. Open your web browser and navigate to https://localhost:7274.

  3. Sign-in with an account registered to the external tenant.

  4. Once signed in the display name is shown next to the Sign out button as shown in the following screenshot.

    :::image type="content" source="media/tutorial-web-app-dotnet-sign-in-sign-in-out/display-aspnet-welcome.png" alt-text="Screenshot of sign in into a ASP.NET Core web app.":::

  5. To sign out from the application, select the Sign out button.

See also