|
| 1 | + |
1 | 2 | ## How the code was created
|
2 | 3 |
|
3 | 4 | <details>
|
4 | 5 | <summary>Expand the section</summary>
|
5 | 6 |
|
| 7 | +### Creating the Web API project (TodoListService) |
| 8 | + |
| 9 | +The code for the TodoListService was created in the following way: |
| 10 | + |
| 11 | +#### Step 1: Create the web api using the ASP.NET Core templates |
| 12 | + |
| 13 | +```Text |
| 14 | +md TodoListService |
| 15 | +cd TodoListService |
| 16 | +dotnet new webapi -au=SingleOrg |
| 17 | +``` |
| 18 | + |
| 19 | +1. Open the generated project (.csproj) in Visual Studio, and save the solution. |
| 20 | + |
| 21 | +#### Add a model (TodoListItem) and modify the controller |
| 22 | + |
| 23 | +In the TodoListService project, add a folder named `Models` and then create a new file named `TodoItem.cs`. Copy the contents of the TodoListService\Models\TodoItem.cs in this file. |
| 24 | + |
| 25 | +### Modify the Program.cs file to validate bearer access tokens received by the Web API |
| 26 | + |
| 27 | +Update `Program.cs` file : |
| 28 | + |
| 29 | + *replace the following code: |
| 30 | + |
| 31 | + ```CSharp |
| 32 | +builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) |
| 33 | + .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd")); |
| 34 | + ``` |
| 35 | + |
| 36 | + with |
| 37 | + |
| 38 | + ```Csharp |
| 39 | + services.AddMicrosoftIdentityWebApiAuthentication(Configuration); |
| 40 | + ``` |
| 41 | + |
| 42 | +### Create the TodoListController and associated Models |
| 43 | + |
| 44 | +1. Add a reference to the ToDoListService. |
| 45 | +1. Create a new Controller named `TodoListController` and copy and paste the code from the sample (TodoListService\Controllers\TodoListController.cs) to this controller. |
| 46 | +1. Open the `appsettings.json` file and copy the keys from the sample's corresponding file under the `AzureAd` and `TodoList` sections. |
| 47 | + |
6 | 48 | ### Creating the client web app (TodoListClient)
|
7 | 49 |
|
8 | 50 | #### Step 1: the sample from the command line
|
|
20 | 62 | #### Step 2: Modify the generated code
|
21 | 63 |
|
22 | 64 | 1. Open the generated project (.csproj) in Visual Studio, and save the solution.
|
23 |
| -1. Add the `Microsoft.Identity.Web.csproj` project which is located at the root of this sample repo, to your solution (**Add Existing Project ...**). It's used to simplify signing-in and, in the next tutorial phases, to get a token. |
24 |
| -1. Add a reference from your newly generated project to `Microsoft.Identity.Web` (right click on the **Dependencies** node under your new project, and choose **Add Reference ...**, and then in the projects tab find the `Microsoft.Identity.Web` project) |
25 |
| -
|
26 |
| -1. Open the **Startup.cs** file and: |
| 65 | +1. Add the `Microsoft.Identity.Web` via Nuget. |
| 66 | +1. Open the **Program.cs** file and: |
27 | 67 |
|
28 |
| - * at the top of the file, add the following using directive were added: |
| 68 | + * Replace the two following lines: |
29 | 69 |
|
30 |
| - ```CSharp |
31 |
| - using Microsoft.Identity.Web; |
32 |
| - ``` |
33 |
| -
|
34 |
| - * in the `ConfigureServices` method, replace the two following lines: |
35 |
| -
|
36 |
| - ```CSharp |
37 |
| - services.AddAuthentication(AzureADDefaults.AuthenticationScheme) |
38 |
| - .AddAzureAD(options => Configuration.Bind("AzureAd", options)); |
39 |
| - ``` |
| 70 | +```CSharp |
| 71 | +services.AddAuthentication(AzureADDefaults.AuthenticationScheme) |
| 72 | + .AddAzureAD(options => Configuration.Bind("AzureAd", options)); |
| 73 | +``` |
40 | 74 |
|
41 |
| - with these lines: |
| 75 | +with these lines: |
42 | 76 |
|
43 |
| - ```CSharp |
44 |
| - services.AddMicrosoftIdentityWebAppAuthentication(Configuration) |
45 |
| - .EnableTokenAcquisitionToCallDownstreamApi( |
46 |
| - Configuration.GetSection("TodoList:TodoListScopes").Get<string>().Split(" ", System.StringSplitOptions.RemoveEmptyEntries)) |
47 |
| - .AddInMemoryTokenCaches(); |
48 |
| - ``` |
| 77 | +```CSharp |
| 78 | +services.AddMicrosoftIdentityWebAppAuthentication(Configuration) |
| 79 | + .EnableTokenAcquisitionToCallDownstreamApi( |
| 80 | + Configuration.GetSection("TodoList:TodoListScopes").Get<string>().Split(" ", System.StringSplitOptions.RemoveEmptyEntries) |
| 81 | + ) |
| 82 | + .AddInMemoryTokenCaches(); |
| 83 | +``` |
49 | 84 |
|
50 |
| - This enables your application to use the Microsoft identity platform endpoint. This endpoint is capable of signing-in users both with their Work and School and Microsoft Personal accounts. |
| 85 | +* This enables your application to use the Microsoft identity platform endpoint. This endpoint is capable of signing-in users both with their Work and School and Microsoft Personal accounts. |
51 | 86 |
|
52 | 87 | 1. Change the `Properties\launchSettings.json` file to ensure that you start your web app from <https://localhost:44321> as registered. For this:
|
53 | 88 | * update the `sslPort` of the `iisSettings` section to be `44321`
|
|
59 | 94 | services.AddTodoListService(Configuration);
|
60 | 95 | ```
|
61 | 96 |
|
62 |
| -1. Open the `appsettings.json` file and copy the keys from the sample's corresponding file under the `AzureAd` and `TodoList` sections. |
| 97 | +2. Open the `appsettings.json` file and copy the keys from the sample's corresponding file under the `AzureAd` and `TodoList` sections. |
63 | 98 |
|
64 | 99 | #### Add a model (TodoListItem) and add the controller and views
|
65 | 100 |
|
|
82 | 117 | services.AddTodoListService(Configuration);
|
83 | 118 | ```
|
84 | 119 |
|
85 |
| -1. Update the `Configure` method to include **app.UseAuthentication();** before **app.UseAuthorization();** |
86 |
| - |
87 |
| - ```Csharp |
88 |
| - app.UseAuthentication(); |
89 |
| - app.AddAuthorization(); |
90 |
| - ``` |
91 |
| - |
92 |
| -### Creating the Web API project (TodoListService) |
93 |
| - |
94 |
| -The code for the TodoListService was created in the following way: |
95 |
| - |
96 |
| -#### Step 1: Create the web api using the ASP.NET Core templates |
97 |
| - |
98 |
| -```Text |
99 |
| -md TodoListService |
100 |
| -cd TodoListService |
101 |
| -dotnet new webapi -au=SingleOrg |
102 |
| -``` |
103 |
| - |
104 |
| -1. Open the generated project (.csproj) in Visual Studio, and save the solution. |
105 |
| - |
106 |
| -#### Add a model (TodoListItem) and modify the controller |
107 |
| - |
108 |
| -In the TodoListService project, add a folder named `Models` and then create a new file named `TodoItem.cs`. Copy the contents of the TodoListService\Models\TodoItem.cs in this file. |
109 |
| - |
110 |
| -### Modify the Startup.cs file to validate bearer access tokens received by the Web API |
111 |
| - |
112 |
| -1. Add the `Microsoft.Identity.Web.csproj` project which is located at the root of this sample repo, to your solution (**Add Existing Project ...**). |
113 |
| -1. Add a reference from your newly generated project to `Microsoft.Identity.Web` (right click on the **Dependencies** node under your new project, and choose **Add Reference ...**, and then in the projects tab find the `Microsoft.Identity.Web` project) |
114 |
| -Update `Startup.cs` file : |
115 |
| - |
116 |
| - *Add the following two using statements |
117 |
| - |
118 |
| -```CSharp |
119 |
| -using Microsoft.Identity.Web; |
120 |
| -using Microsoft.Identity.Web.Client.TokenCacheProviders; |
121 |
| -``` |
122 |
| - |
123 |
| - *In the `ConfigureServices` method, replace the following code: |
124 |
| - |
125 |
| - ```CSharp |
126 |
| - services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme) |
127 |
| - .AddAzureADBearer(options => Configuration.Bind("AzureAd", options)); |
128 |
| - ``` |
129 |
| - |
130 |
| - with |
131 |
| - |
132 |
| - ```Csharp |
133 |
| - services.AddMicrosoftIdentityWebApiAuthentication(Configuration); |
134 |
| - ``` |
135 |
| - |
136 |
| - *Add the method **app.UseAuthentication()** before **app.UseAuthorization()** in the `Configure` method |
137 |
| - |
138 |
| - ```Csharp |
139 |
| - app.UseAuthentication(); |
140 |
| - app.UseAuthorization(); |
141 |
| - ``` |
142 |
| - |
143 |
| -### Create the TodoListController.cs file |
144 |
| - |
145 |
| -1. Add a folder named `Models` and then create a new file named `TodoItem.cs`. Copy the contents of the TodoListClient\Models\TodoItem.cs in this file. |
146 |
| -1. Create a new Controller named `TodoListController` and copy and paste the code from the sample (TodoListService\Controllers\TodoListController.cs) to this controller. |
147 |
| - |
148 | 120 | </details>
|
0 commit comments