|
| 1 | +--- |
| 2 | +title: "Tutorial: Create and configure an ASP.NET Core project for authentication" |
| 3 | +description: "Create and configure the API in an IDE, add configuration for authentication and install required packages" |
| 4 | +author: cilwerner |
| 5 | +ms.author: cwerner |
| 6 | +manager: CelesteDG |
| 7 | +ms.service: active-directory |
| 8 | +ms.topic: tutorial |
| 9 | +ms.date: 11/1/2022 |
| 10 | +#Customer intent: As an application developer, I want to create an ASP.NET Core project in an IDE, then configure it in such a way that I can add authentication with Azure AD. |
| 11 | +--- |
| 12 | + |
| 13 | +# Tutorial: Create and configure an ASP.NET Core project for authentication |
| 14 | + |
| 15 | +After registration is complete, a ASP.NET Core project can be created using an integrated development environment (IDE). This tutorial demonstrates how to create an ASP.NET Core project using an IDE and configure for authentication and authorization. |
| 16 | + |
| 17 | +In this tutorial: |
| 18 | + |
| 19 | +> [!div class="checklist"] |
| 20 | +> * Create an **ASP.NET Core Empty** |
| 21 | +> * Configure the settings for the application |
| 22 | +> * Identify and install the required NuGet packages |
| 23 | +
|
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +* Completion of the prerequisites and steps in [Tutorial: Register web API with the Microsoft identity platform](web-api-tutorial-01-register-app.md). |
| 27 | +* You can download the IDEs used in this tutorial from the [Downloads](https://visualstudio.microsoft.com/downloads) page. |
| 28 | + - Visual Studio 2022 |
| 29 | + - Visual Studio Code |
| 30 | + - Visual Studio 2022 for Mac |
| 31 | +- A minimum requirement of [.NET Core 6.0 SDK](https://dotnet.microsoft.com/download/dotnet). |
| 32 | + |
| 33 | +## Create an ASP.NET Core project |
| 34 | + |
| 35 | +Use the following tabs to create an ASP.NET Core project within an IDE. |
| 36 | + |
| 37 | +### [Visual Studio](#tab/visual-studio) |
| 38 | + |
| 39 | +1. Open Visual Studio, and then select **Create a new project**. |
| 40 | +1. Search for and choose the **ASP.NET Core Empty** template, and then select **Next**. |
| 41 | +1. Enter a name for the project, such as *NewWebAPILocal*. |
| 42 | +1. Choose a location for the project or accept the default option, and then select **Next**. |
| 43 | +1. Accept the default for the **Framework** and **Configure for HTTPS**. |
| 44 | +1. Select **Create**. |
| 45 | + |
| 46 | +### [Visual Studio Code](#tab/visual-studio-code) |
| 47 | + |
| 48 | +1. Open Visual Studio Code, select **File > Open Folder...**. Navigate to and select the location in which to create your project. |
| 49 | +1. Open up a new terminal by selecting **Terminal** in the top bar, then **New Terminal**. |
| 50 | +1. Create a new folder using the **New Folder...** icon in the **Explorer** pane. Provide a name similar to the one registered previously, for example, *NewWebAPILocal*. |
| 51 | +1. Open a new terminal by selecting **Terminal > New Terminal**. |
| 52 | +1. To create an **ASP.NET Core Empty** template, run the following commands in the terminal to change into the directory and create the project: |
| 53 | + |
| 54 | + ```powershell |
| 55 | + cd NewWebAPILocal |
| 56 | + dotnet new web |
| 57 | + ``` |
| 58 | +
|
| 59 | +### [Visual Studio for Mac](#tab/visual-studio-for-mac) |
| 60 | +
|
| 61 | +1. Open Visual Studio, and then select **New**. |
| 62 | +1. Under **Web and Console** in the left navigation bar, select **App**. |
| 63 | +1. Under **ASP.NET Core**, select **API** and ensure **C#** is selected in the drop down menu, then select **Continue**. |
| 64 | +1. Accept the default for the **Target Framework** and **Advanced**, then select **Continue**. |
| 65 | +1. Enter a name for the **Project name**, this will be reflected in **Solution Name**. Provide a similar name to the one registered on the Azure portal, such as *NewAPI1*. |
| 66 | +1. Accept the default location for the project or choose a different location, and then select **Create**. |
| 67 | +--- |
| 68 | +
|
| 69 | +## Configure the ASP.NET Core project |
| 70 | +
|
| 71 | +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. |
| 72 | +
|
| 73 | +1. Open *appsettings.json* and replace the file contents with the following code snippet: |
| 74 | +
|
| 75 | + ```json |
| 76 | + { |
| 77 | + "AzureAd": { |
| 78 | + "Instance": "https://login.microsoftonline.com/", |
| 79 | + "ClientId": "Enter the client ID here", |
| 80 | + "TenantId": "Enter the tenant ID here", |
| 81 | + "Scopes": "Forecast.Read" |
| 82 | + }, |
| 83 | + "Logging": { |
| 84 | + "LogLevel": { |
| 85 | + "Default": "Information", |
| 86 | + "Microsoft.AspNetCore": "Warning" |
| 87 | + } |
| 88 | + }, |
| 89 | + "AllowedHosts": "*" |
| 90 | + } |
| 91 | + ``` |
| 92 | +
|
| 93 | + * `Instance` - The endpoint of the cloud provider. Check with the different available endpoints in [National clouds](authentication-national-cloud.md#azure-ad-authentication-endpoints). |
| 94 | + * `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. |
| 95 | + * `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. |
| 96 | + * `Scopes` - The scope that is used to request access to the application. For this tutorial, the scope is `Forecast.Read`. |
| 97 | +1. Save the changes to the file. |
| 98 | +
|
| 99 | +## Install identity packages |
| 100 | +
|
| 101 | +Identity related **NuGet packages** must be installed in the project for authentication of users to be enabled. |
| 102 | +
|
| 103 | +### [Visual Studio](#tab/visual-studio) |
| 104 | +
|
| 105 | +1. In the top menu, select **Tools** > **NuGet Package Manager** > **Manage NuGet Packages for Solution**. |
| 106 | +1. With the **Browse** tab selected, search for **Microsoft.Identity.Web**, select the `Microsoft.Identity.Web` package, select the **Project** checkbox, and then select **Install**. |
| 107 | +1. Select **Ok** or **I Accept** for other windows that may appear. |
| 108 | +
|
| 109 | +### [Visual Studio Code](#tab/visual-studio-code) |
| 110 | +
|
| 111 | +1. In the terminal opened in the previous section, enter the following command: |
| 112 | +
|
| 113 | + ```powershell |
| 114 | + dotnet add package Microsoft.Identity.Web |
| 115 | + ``` |
| 116 | +
|
| 117 | +### [Visual Studio for Mac](#tab/visual-studio-for-mac) |
| 118 | +
|
| 119 | +1. In the top menu, select **Tools** > **Manage NuGet Packages**. |
| 120 | +1. Search for **Microsoft.Identity.Web**, select the `Microsoft.Identity.Web` package, select **Project**, and then select **Add Package**. |
| 121 | +1. Modify the search to **Microsoft.Identity.Web.UI** and select **Add Packages**. |
| 122 | +1. In the pop-up, ensure the correct project is selected, then select **Ok**. |
| 123 | +1. Select **Accept** if other **License Acceptance** windows appear. |
| 124 | +
|
| 125 | +--- |
| 126 | +
|
| 127 | +## Next steps |
| 128 | +
|
| 129 | +> [!div class="nextstepaction"] |
| 130 | +> [Tutorial: Implement a protected endpoint to your API](web-api-tutorial-03-protect-endpoint.md) |
0 commit comments