Skip to content

Commit 3753857

Browse files
author
Kalyan Krishna
committed
WIP
1 parent 030c164 commit 3753857

File tree

9 files changed

+165
-7
lines changed

9 files changed

+165
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,4 @@
8484
/4-WebApp-your-API/4-1-Your-API/.vs/WebApp-OpenIDConnect-DotNet/v16
8585
/4-WebApp-your-API/4-1-Your-API/Client/bin/Debug/netcoreapp2.2
8686
/4-WebApp-your-API/4-1-Your-API/Client/obj
87+
/4-WebApp-your-API/4-1-Your-API/.vs/WebApp-OpenIDConnect-DotNet
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Identity.Web.Client;
8+
9+
namespace TodoListClient.Controllers
10+
{
11+
public class TodoListController : Controller
12+
{
13+
ITokenAcquisition _tokenAcquisition;
14+
15+
public TodoListController(ITokenAcquisition tokenAcquisition)
16+
{
17+
_tokenAcquisition = tokenAcquisition;
18+
}
19+
20+
// GET: TodoList
21+
public ActionResult Index()
22+
{
23+
return View();
24+
}
25+
26+
// GET: TodoList/Details/5
27+
public ActionResult Details(int id)
28+
{
29+
return View();
30+
}
31+
32+
// GET: TodoList/Create
33+
public ActionResult Create()
34+
{
35+
return View();
36+
}
37+
38+
// POST: TodoList/Create
39+
[HttpPost]
40+
[ValidateAntiForgeryToken]
41+
public ActionResult Create(IFormCollection collection)
42+
{
43+
try
44+
{
45+
// TODO: Add insert logic here
46+
47+
return RedirectToAction(nameof(Index));
48+
}
49+
catch
50+
{
51+
return View();
52+
}
53+
}
54+
55+
// GET: TodoList/Edit/5
56+
public ActionResult Edit(int id)
57+
{
58+
return View();
59+
}
60+
61+
// POST: TodoList/Edit/5
62+
[HttpPost]
63+
[ValidateAntiForgeryToken]
64+
public ActionResult Edit(int id, IFormCollection collection)
65+
{
66+
try
67+
{
68+
// TODO: Add update logic here
69+
70+
return RedirectToAction(nameof(Index));
71+
}
72+
catch
73+
{
74+
return View();
75+
}
76+
}
77+
78+
// GET: TodoList/Delete/5
79+
public ActionResult Delete(int id)
80+
{
81+
return View();
82+
}
83+
84+
// POST: TodoList/Delete/5
85+
[HttpPost]
86+
[ValidateAntiForgeryToken]
87+
public ActionResult Delete(int id, IFormCollection collection)
88+
{
89+
try
90+
{
91+
// TODO: Add delete logic here
92+
93+
return RedirectToAction(nameof(Index));
94+
}
95+
catch
96+
{
97+
return View();
98+
}
99+
}
100+
}
101+
}

4-WebApp-your-API/4-1-Your-API/Client/Infrastructure/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace WebApp_OpenIDConnect_DotNet.Infrastructure
22
{
33
public static class Constants
44
{
5-
public const string ScopeUserRead = "User.Read";
5+
public const string ScopeUserImpersonation = "user_impersonation";
66
public const string BearerAuthorizationScheme = "Bearer";
77
}
88
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace TodoListClient.Models
7+
{
8+
public class Todo
9+
{
10+
public string Title { get; set; }
11+
}
12+
}

4-WebApp-your-API/4-1-Your-API/Client/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:3110/",
6+
"applicationUrl": "https://localhost:44321/",
77
"sslPort": 44321
88
}
99
},

4-WebApp-your-API/4-1-Your-API/Client/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void ConfigureServices(IServiceCollection services)
3737
// Token acquisition service based on MSAL.NET
3838
// and chosen token cache implementation
3939
services.AddAzureAdV2Authentication(Configuration)
40-
.AddMsal(new string[] { Constants.ScopeUserRead })
40+
.AddMsal(new string[] { Constants.ScopeUserImpersonation })
4141
.AddInMemoryTokenCaches();
4242

4343
// Add APIs

4-WebApp-your-API/4-1-Your-API/Client/TodoListClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<PackageReference Include="Microsoft.AspNetCore.App" />
2222
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.2.0" />
2323
<PackageReference Include="Microsoft.Graph" Version="1.12.0" />
24+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
2425
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
2526
</ItemGroup>
2627

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@model IEnumerable<TodoListClient.Models.Todo>
2+
3+
@{
4+
ViewData["Title"] = "Index";
5+
}
6+
7+
<h2>Index</h2>
8+
9+
<p>
10+
<a asp-action="Create">Create New</a>
11+
</p>
12+
<table class="table">
13+
<thead>
14+
<tr>
15+
<th>
16+
@Html.DisplayNameFor(model => model.Title)
17+
</th>
18+
<th></th>
19+
</tr>
20+
</thead>
21+
<tbody>
22+
@foreach (var item in Model) {
23+
<tr>
24+
<td>
25+
@Html.DisplayFor(modelItem => item.Title)
26+
</td>
27+
<td>
28+
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
29+
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
30+
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
31+
</td>
32+
</tr>
33+
}
34+
</tbody>
35+
</table>

4-WebApp-your-API/4-1-Your-API/Client/appsettings.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@
33
"Instance": "https://login.microsoftonline.com/",
44
"Domain": "[Enter the domain of your tenant, e.g. contoso.onmicrosoft.com]",
55
"TenantId": "[Enter 'common', or 'organizations' or the Tenant Id (Obtained from the Azure portal. Select 'Endpoints' from the 'App registrations' blade and use the GUID in any of the URLs), e.g. da41245a5-11b3-996c-00a8-4d99re19f292]",
6-
"ClientId": "[Enter the Client Id (Application ID obtained from the Azure portal), e.g. ba74781c2-53c2-442a-97c2-3d60re42f403]",
6+
"ClientId": "[Enter the Client Id of the web app (Application ID obtained from the Azure portal), e.g. ba74781c2-53c2-442a-97c2-3d60re42f403]",
77
"CallbackPath": "/signin-oidc",
88
"SignedOutCallbackPath ": "/signout-callback-oidc",
99

1010
// To call an API
11-
"ClientSecret": "[Copy the client secret added to the app from the Azure portal]"
11+
"ClientSecret": "[Copy the client secret added to the app from the Azure portal]",
12+
13+
/*
14+
TodoListScope is the scope of the Web API you want to call. This can be: "api://bf3599a0-9ea9-4532-b623-4351a8c7ee74/user_impersonation",
15+
- a scope for a V2 application (for instance api://b3682cc7-8b30-4bd2-aaba-080c6bf0fd31/access_as_user)
16+
- a scope corresponding to a V1 application (for instance <GUID>/user_impersonation, where <GUID> is the
17+
clientId of a V1 application, created in the https://portal.azure.com portal.
18+
*/
19+
"TodoListScope": "api://[Enter_client_ID_Of_TodoListService-v2_from_Azure_Portal,_e.g._2ec40e65-ba09-4853-bcde-bcb60029e596]/user_impersonation",
20+
"TodoListBaseAddress": "https://localhost:44351/"
1221

1322
},
1423
"Logging": {
1524
"LogLevel": {
1625
"Default": "Warning"
1726
}
1827
},
19-
"AllowedHosts": "*",
20-
"GraphApiUrl": "https://graph.microsoft.com"
28+
"AllowedHosts": "*"
2129
}

0 commit comments

Comments
 (0)