ערוך

שתף באמצעות


Tutorial: Create and configure an ASP.NET Core project for authentication

In this tutorial, you learn how to create an ASP.NET Core project using an IDE and configure it for authentication and authorization. This tutorial is the second part of a series that demonstrates how to secure a web API using the Microsoft identity platform. In the previous article, you registered an application in your Microsoft Entra ID tenant. In this article, you;

  • Create an ASP.NET Core Empty project in your IDE
  • Configure the settings for the application
  • Identify and install the required NuGet packages

Prerequisites

Create an ASP.NET Core project

Use the following tabs to create an ASP.NET Core project within an IDE.

  1. Open Visual Studio, and then select Create a new project.
  2. Search for and choose the ASP.NET Core Empty template, and then select Next.
  3. Enter a name for the project, such as NewWebAPILocal.
  4. Choose a location for the project or accept the default option, and then select Next.
  5. Accept the default for the Framework and Configure for HTTPS.
  6. Select Create.

Configure the ASP.NET Core project

The values recorded earlier will be used in appsettings.json to configure the application for authentication. appsettings.json is a configuration file that is used to store application settings used during run-time.

  1. Open appsettings.json and replace the file contents with the following code snippet:

    {
      "AzureAd": {
        "Instance": "https://login.microsoftonline.com/",
        "ClientId": "Enter the client ID here",
        "TenantId": "Enter the tenant ID here",
        "Scopes": "Forecast.Read"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
      },
      "AllowedHosts": "*"
    } 
    
    • Instance - The endpoint of the cloud provider. Check with the different available endpoints in National clouds.
    • TenantId - The identifier of the tenant where the application is registered. Replace the text in quotes with the Directory (tenant) ID value 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.
    • Scopes - The scope that is used to request access to the application. For this tutorial, the scope is Forecast.Read.
  2. Save the changes to the file.

Install identity packages

Identity related NuGet packages must be installed in the project for authentication of users to be enabled.

  1. In the top menu, select Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
  2. With the Browse tab selected, search for Microsoft.Identity.Web, select the Microsoft.Identity.Web package, select the Project checkbox, and then select Install.
  3. Select Ok or I Accept for other windows that may appear.

Next steps