Skip to content

Commit 871d02e

Browse files
author
Kalyan Krishna
committed
Ready for review
1 parent 35a7131 commit 871d02e

File tree

22 files changed

+1225
-1098
lines changed

22 files changed

+1225
-1098
lines changed

4-WebApp-your-API/4-1-Your-API/AppCreationScripts/Cleanup.ps1

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,25 @@ This function removes the Azure AD applications for the sample. These applicatio
4949
# Removes the applications
5050
Write-Host "Cleaning-up applications from tenant '$tenantName'"
5151

52-
Write-Host "Removing 'service' (TodoListService (active-directory-aspnetcore-webapp-openidconnect-v2)) if needed"
53-
Get-AzureADApplication -Filter "DisplayName eq 'TodoListService (active-directory-aspnetcore-webapp-openidconnect-v2)'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54-
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListService (active-directory-aspnetcore-webapp-openidconnect-v2)'"
52+
Write-Host "Removing 'service' (TodoListService-aspnetcore-webapi) if needed"
53+
Get-AzureADApplication -Filter "DisplayName eq 'TodoListService-aspnetcore-webapi'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListService-aspnetcore-webapi'"
5555
if ($apps)
5656
{
5757
Remove-AzureADApplication -ObjectId $apps.ObjectId
5858
}
59-
Get-AzureRmADServicePrincipal -SearchString "TodoListService (active-directory-aspnetcore-webapp-openidconnect-v2)" | ForEach-Object {Remove-AzureRmADServicePrincipal -ObjectId $_.Id -Confirm:$false}
60-
Write-Host "Removed TodoListService (active-directory-aspnetcore-webapp-openidconnect-v2)."
59+
# Get-AzureRmADServicePrincipal -SearchString "TodoListService-aspnetcore-webapi" | ForEach-Object {Remove-AzureRmADServicePrincipal -ObjectId $_.Id -Confirm:$false}
60+
Write-Host "Removed TodoListService-aspnetcore-webapi."
6161

62-
Write-Host "Removing 'client' (TodoListClient (active-directory-aspnetcore-webapp-openidconnect-v2)) if needed"
63-
Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient (active-directory-aspnetcore-webapp-openidconnect-v2)'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
64-
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient (active-directory-aspnetcore-webapp-openidconnect-v2)'"
62+
Write-Host "Removing 'client' (TodoListClient-aspnetcore-webapi) if needed"
63+
Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient-aspnetcore-webapi'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
64+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'TodoListClient-aspnetcore-webapi'"
6565
if ($apps)
6666
{
6767
Remove-AzureADApplication -ObjectId $apps.ObjectId
6868
}
69-
Get-AzureRmADServicePrincipal -SearchString "TodoListClient (active-directory-aspnetcore-webapp-openidconnect-v2)" | ForEach-Object {Remove-AzureRmADServicePrincipal -ObjectId $_.Id -Confirm:$false}
70-
Write-Host "Removed TodoListClient (active-directory-aspnetcore-webapp-openidconnect-v2)."
69+
# Get-AzureRmADServicePrincipal -SearchString "TodoListClient-aspnetcore-webapi" | ForEach-Object {Remove-AzureRmADServicePrincipal -ObjectId $_.Id -Confirm:$false}
70+
Write-Host "Removed TodoListClient-aspnetcore-webapi."
7171

7272
}
7373

4-WebApp-your-API/4-1-Your-API/AppCreationScripts/Configure.ps1

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ Function ConfigureApplications
145145
so that they are consistent with the Applications parameters
146146
#>
147147

148-
$commonendpoint = "common"
149-
150148
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
151149
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
152150

@@ -180,8 +178,8 @@ Function ConfigureApplications
180178
$user = Get-AzureADUser -ObjectId $creds.Account.Id
181179

182180
# Create the service AAD application
183-
Write-Host "Creating the AAD application (TodoListService (active-directory-aspnetcore-webapp-openidconnect-v2))"
184-
$serviceAadApplication = New-AzureADApplication -DisplayName "TodoListService (active-directory-aspnetcore-webapp-openidconnect-v2)" `
181+
Write-Host "Creating the AAD application (TodoListService-aspnetcore-webapi)"
182+
$serviceAadApplication = New-AzureADApplication -DisplayName "TodoListService-aspnetcore-webapi" `
185183
-HomePage "https://localhost:44351/" `
186184
-PublicClient $False
187185
$serviceIdentifierUri = 'api://'+$serviceAadApplication.AppId
@@ -194,29 +192,29 @@ Function ConfigureApplications
194192
$owner = Get-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId
195193
if ($owner -eq $null)
196194
{
197-
Add-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId -RefObjectId $user.ObjectId
198-
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($serviceServicePrincipal.DisplayName)'"
195+
Add-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId -RefObjectId $user.ObjectId
196+
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($serviceServicePrincipal.DisplayName)'"
199197
}
200198

201-
Write-Host "Done creating the service application (TodoListService (active-directory-aspnetcore-webapp-openidconnect-v2))"
199+
Write-Host "Done creating the service application (TodoListService-aspnetcore-webapi)"
202200

203201
# URL of the AAD application in the Azure portal
204202
# Future? $servicePortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
205203
$servicePortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
206-
Add-Content -Value "<tr><td>service</td><td>$currentAppId</td><td><a href='$servicePortalUrl'>TodoListService (active-directory-aspnetcore-webapp-openidconnect-v2)</a></td></tr>" -Path createdApps.html
204+
Add-Content -Value "<tr><td>service</td><td>$currentAppId</td><td><a href='$servicePortalUrl'>TodoListService-aspnetcore-webapi</a></td></tr>" -Path createdApps.html
207205

208206
# Create the client AAD application
209-
Write-Host "Creating the AAD application (TodoListClient (active-directory-aspnetcore-webapp-openidconnect-v2))"
207+
Write-Host "Creating the AAD application (TodoListClient-aspnetcore-webapi)"
210208
# Get a 2 years application key for the client Application
211209
$pw = ComputePassword
212210
$fromDate = [DateTime]::Now;
213211
$key = CreateAppKey -fromDate $fromDate -durationInYears 2 -pw $pw
214212
$clientAppKey = $pw
215-
$clientAadApplication = New-AzureADApplication -DisplayName "TodoListClient (active-directory-aspnetcore-webapp-openidconnect-v2)" `
213+
$clientAadApplication = New-AzureADApplication -DisplayName "TodoListClient-aspnetcore-webapi" `
216214
-HomePage "https://localhost:44321/" `
217215
-LogoutUrl "https://localhost:44321/signout-oidc" `
218216
-ReplyUrls "https://localhost:44321/", "https://localhost:44321/signin-oidc" `
219-
-IdentifierUris "https://$tenantName/TodoListClient(active-directory-aspnetcore-webapp-openidconnect-v2)" `
217+
-IdentifierUris "https://$tenantName/TodoListClient-aspnetcore-webapi" `
220218
-PasswordCredentials $key `
221219
-Oauth2AllowImplicitFlow $true `
222220
-PublicClient $False
@@ -228,22 +226,22 @@ Function ConfigureApplications
228226
$owner = Get-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId
229227
if ($owner -eq $null)
230228
{
231-
Add-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId -RefObjectId $user.ObjectId
232-
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($clientServicePrincipal.DisplayName)'"
229+
Add-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId -RefObjectId $user.ObjectId
230+
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($clientServicePrincipal.DisplayName)'"
233231
}
234232

235-
Write-Host "Done creating the client application (TodoListClient (active-directory-aspnetcore-webapp-openidconnect-v2))"
233+
Write-Host "Done creating the client application (TodoListClient-aspnetcore-webapi)"
236234

237235
# URL of the AAD application in the Azure portal
238236
# Future? $clientPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId+"/isMSAApp/"
239237
$clientPortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId+"/isMSAApp/"
240-
Add-Content -Value "<tr><td>client</td><td>$currentAppId</td><td><a href='$clientPortalUrl'>TodoListClient (active-directory-aspnetcore-webapp-openidconnect-v2)</a></td></tr>" -Path createdApps.html
238+
Add-Content -Value "<tr><td>client</td><td>$currentAppId</td><td><a href='$clientPortalUrl'>TodoListClient-aspnetcore-webapi</a></td></tr>" -Path createdApps.html
241239

242240
$requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess]
243241

244242
# Add Required Resources Access (from 'client' to 'service')
245243
Write-Host "Getting access from 'client' to 'service'"
246-
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "TodoListService (active-directory-aspnetcore-webapp-openidconnect-v2)" `
244+
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "TodoListService-aspnetcore-webapi" `
247245
-requiredDelegatedPermissions "user_impersonation" `
248246

249247
$requiredResourcesAccess.Add($requiredPermissions)
@@ -263,7 +261,7 @@ Function ConfigureApplications
263261
Write-Host "Updating the sample code ($configFile)"
264262
$dictionary = @{ "Domain" = $tenantName;"TenantId" = $tenantId;"ClientId" = $clientAadApplication.AppId;"ClientSecret" = $clientAppKey;"TodoListScope" = ("api://"+$serviceAadApplication.AppId+"/user_impersonation");"TodoListBaseAddress" = $serviceAadApplication.HomePage };
265263
UpdateTextFile -configFilePath $configFile -dictionary $dictionary
266-
264+
267265
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
268266
}
269267

4-WebApp-your-API/4-1-Your-API/AppCreationScripts/sample.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"Sample": {
3-
"Title": "How to secure a Web API built using ASP.NET Core using Microsoft identity platform (formerly Azure Active Directory for developers)",
3+
"Title": "How to secure a Web API built with ASP.NET Core using the Microsoft identity platform (formerly Azure Active Directory for developers)",
44
"Level": 200,
55
"Client": "ASP.NET Core Web App",
66
"Service": "ASP.NET Core Web API",
@@ -14,15 +14,15 @@
1414
"AADApps": [
1515
{
1616
"Id": "service",
17-
"Name": "TodoListService (active-directory-aspnetcore-webapp-openidconnect-v2)",
17+
"Name": "TodoListService-aspnetcore-webapi",
1818
"Kind": "WebApi",
1919
"Audience": "AzureADMyOrg",
2020
"HomePage": "https://localhost:44351/"
2121

2222
},
2323
{
2424
"Id": "client",
25-
"Name": "TodoListClient (active-directory-aspnetcore-webapp-openidconnect-v2)",
25+
"Name": "TodoListClient-aspnetcore-webapi",
2626
"Kind": "WebApp",
2727
"Audience": "AzureADMyOrg",
2828
"HomePage": "https://localhost:44321/",
Lines changed: 24 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,32 @@
1-
using System;
2-
using System.Collections;
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
33
using System.Collections.Generic;
4-
using System.Linq;
54
using System.Threading.Tasks;
6-
using Microsoft.AspNetCore.Http;
7-
using Microsoft.AspNetCore.Mvc;
8-
using Microsoft.Graph;
9-
using Microsoft.Identity.Web.Client;
5+
using TodoListClient.Services;
106
using TodoListService.Models;
11-
using TodoListClient.Utils;
127

138
namespace TodoListClient.Controllers
149
{
1510
public class TodoListController : Controller
1611
{
17-
ITokenAcquisition _tokenAcquisition;
18-
IList<Todo> Model = new List<Todo>();
12+
private ITodoListService _todoListService;
13+
private IList<Todo> Model = new List<Todo>();
1914

20-
public TodoListController(ITokenAcquisition tokenAcquisition)
15+
public TodoListController(ITodoListService todoListService)
2116
{
22-
_tokenAcquisition = tokenAcquisition;
23-
24-
17+
_todoListService = todoListService;
2518
}
2619

2720
// GET: TodoList
28-
public ActionResult Index()
21+
public async Task<ActionResult> Index()
2922
{
30-
if (HttpContext.Session.Get<IList<Todo>>("ToDoList") == null)
31-
{
32-
Model.Add(new Todo() { Id = 1, Owner = "[email protected]", Title = "do something" });
33-
Model.Add(new Todo() { Id = 2, Owner = "[email protected]", Title = "do something else" });
34-
35-
HttpContext.Session.Set<IList<Todo>>("ToDoList", Model);
36-
}
37-
38-
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
39-
40-
return View(Model);
23+
return View(await _todoListService.GetAsync());
4124
}
4225

4326
// GET: TodoList/Details/5
44-
public ActionResult Details(int id)
27+
public async Task<ActionResult> Details(int id)
4528
{
46-
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
47-
48-
return View(Model.FirstOrDefault(x => x.Id == id));
29+
return View(await _todoListService.GetAsync(id));
4930
}
5031

5132
// GET: TodoList/Create
@@ -58,31 +39,16 @@ public ActionResult Create()
5839
// POST: TodoList/Create
5940
[HttpPost]
6041
[ValidateAntiForgeryToken]
61-
public ActionResult Create([Bind("Title,Owner")] Todo todo)
42+
public async Task<ActionResult> Create([Bind("Title,Owner")] Todo todo)
6243
{
63-
try
64-
{
65-
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
66-
67-
int id = Model.OrderByDescending(x => x.Id).FirstOrDefault().Id + 1;
68-
69-
Model.Add(new Todo() { Id = id, Owner = HttpContext.User.Identity.Name, Title = todo.Title });
70-
HttpContext.Session.Set<IList<Todo>>("ToDoList", Model);
71-
72-
return RedirectToAction(nameof(Index));
73-
}
74-
catch
75-
{
76-
return View();
77-
}
44+
await _todoListService.AddAsync(todo);
45+
return RedirectToAction("Index");
7846
}
7947

8048
// GET: TodoList/Edit/5
81-
public ActionResult Edit(int id)
49+
public async Task<ActionResult> Edit(int id)
8250
{
83-
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
84-
85-
Todo todo = Model.FirstOrDefault(x => x.Id == id);
51+
Todo todo = await this._todoListService.GetAsync(id);
8652

8753
if (todo == null)
8854
{
@@ -95,40 +61,16 @@ public ActionResult Edit(int id)
9561
// POST: TodoList/Edit/5
9662
[HttpPost]
9763
[ValidateAntiForgeryToken]
98-
public ActionResult Edit(int id, [Bind("Id,Title,Owner")] Todo todo)
64+
public async Task<ActionResult> Edit(int id, [Bind("Id,Title,Owner")] Todo todo)
9965
{
100-
try
101-
{
102-
if (id != todo.Id)
103-
{
104-
return NotFound();
105-
}
106-
107-
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
108-
109-
if (Model.FirstOrDefault(x => x.Id == id) == null)
110-
{
111-
return NotFound();
112-
}
113-
114-
Model.Remove(Model.FirstOrDefault(x => x.Id == id));
115-
Model.Add(todo);
116-
117-
HttpContext.Session.Set<IList<Todo>>("ToDoList", Model);
118-
return RedirectToAction(nameof(Index));
119-
}
120-
catch
121-
{
122-
return View();
123-
}
66+
await _todoListService.EditAsync(todo);
67+
return RedirectToAction("Index");
12468
}
12569

12670
// GET: TodoList/Delete/5
127-
public ActionResult Delete(int id)
71+
public async Task<ActionResult> Delete(int id)
12872
{
129-
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
130-
131-
Todo todo = Model.FirstOrDefault(x => x.Id == id);
73+
Todo todo = await this._todoListService.GetAsync(id);
13274

13375
if (todo == null)
13476
{
@@ -141,31 +83,10 @@ public ActionResult Delete(int id)
14183
// POST: TodoList/Delete/5
14284
[HttpPost]
14385
[ValidateAntiForgeryToken]
144-
public ActionResult Delete(int id, [Bind("Id,Title,Owner")] Todo todo)
86+
public async Task<ActionResult> Delete(int id, [Bind("Id,Title,Owner")] Todo todo)
14587
{
146-
try
147-
{
148-
if (id != todo.Id)
149-
{
150-
return NotFound();
151-
}
152-
153-
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
154-
155-
if (Model.FirstOrDefault(x => x.Id == id) == null)
156-
{
157-
return NotFound();
158-
}
159-
160-
Model.Remove(Model.FirstOrDefault(x => x.Id == id));
161-
HttpContext.Session.Set<IList<Todo>>("ToDoList", Model);
162-
163-
return RedirectToAction(nameof(Index));
164-
}
165-
catch
166-
{
167-
return View();
168-
}
88+
await _todoListService.DeleteAsync(id);
89+
return RedirectToAction("Index");
16990
}
17091
}
17192
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2018 Microsoft Corporation
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
using System;
26+
using System.Collections.Generic;
27+
using System.Linq;
28+
using System.Threading.Tasks;
29+
using TodoListService.Models;
30+
31+
namespace TodoListClient.Services
32+
{
33+
public interface ITodoListService
34+
{
35+
Task<IEnumerable<Todo>> GetAsync();
36+
37+
Task<Todo> GetAsync(int id);
38+
39+
Task DeleteAsync(int id);
40+
41+
Task<Todo> AddAsync(Todo todo);
42+
43+
Task<Todo> EditAsync(Todo todo);
44+
}
45+
}

0 commit comments

Comments
 (0)