Skip to content

Commit 24bc5af

Browse files
committed
review changes
1 parent 90b67c0 commit 24bc5af

File tree

6 files changed

+41
-53
lines changed

6 files changed

+41
-53
lines changed

3-Authorization-II/1-call-api/API/TodoListAPI/Controllers/TodoListController.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -207,26 +207,5 @@ public async Task<ActionResult<TodoItem>> DeleteTodoItem(int id)
207207

208208
return NoContent();
209209
}
210-
211-
//// Checks if the presented token has application permissions
212-
//private bool HasApplicationPermissions(string[] permissionsNames)
213-
//{
214-
// var rolesClaim = User.Claims.Where(
215-
// c => c.Type == ClaimConstants.Roles || c.Type == ClaimConstants.Role)
216-
// .SelectMany(c => c.Value.Split(' '));
217-
218-
// var result = rolesClaim.Any(v => permissionsNames.Any(p => p.Equals(v)));
219-
220-
// return result;
221-
//}
222-
223-
//// Checks if the presented token has delegated permissions
224-
//private bool HasDelegatedPermissions(string[] scopesNames)
225-
//{
226-
// var result = (User.FindFirst(ClaimConstants.Scp) ?? User.FindFirst(ClaimConstants.Scope))?
227-
// .Value.Split(' ').Any(v => scopesNames.Any(s => s.Equals(v)));
228-
229-
// return result ?? false;
230-
//}
231210
}
232211
}

3-Authorization-II/1-call-api/API/TodoListAPI/Startup.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
2-
using System.Linq;
3-
using System.Net;
41
using Microsoft.AspNetCore.Builder;
52
using Microsoft.AspNetCore.Hosting;
63
using Microsoft.Extensions.Hosting;

3-Authorization-II/1-call-api/AppCreationScripts/sample.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@
4343
"Application"
4444
],
4545
"Name": "TodoList.Read.All",
46-
"Description": "Allow this application to read every users Todo list items"
46+
"Description": "Allow this application to read every users TodoList items"
4747
},
4848
{
4949
"AllowedMemberTypes": [
5050
"Application"
5151
],
5252
"Name": "TodoList.ReadWrite.All",
53-
"Description": "Allow this application to read and write every users Todo list items"
53+
"Description": "Allow this application to read and write every users TodoList items"
5454
}
5555
],
5656
"OptionalClaims": {

3-Authorization-II/1-call-api/README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
page_type: sample
3-
name: An Angular SPA using MSAL Angular to sign-in users with Azure Active Directory and call a protected .NET Core web API
3+
name: Angular single-page application using MSAL Angular to sign-in users with Azure Active Directory and call a .NET Core web API
44
services: ms-identity
55
platform: javascript
66
languages:
@@ -14,7 +14,7 @@ urlFragment: ms-identity-javascript-angular-tutorial
1414
description: An Angular SPA using MSAL Angular to sign-in users with Azure Active Directory and call a protected .NET Core web API
1515
---
1616

17-
# An Angular SPA using MSAL Angular to sign-in users with Azure Active Directory and call a protected .NET Core web API
17+
# Angular single-page application using MSAL Angular to sign-in users with Azure Active Directory and call a .NET Core web API
1818

1919
* [Overview](#overview)
2020
* [Scenario](#scenario)
@@ -153,20 +153,20 @@ To manually register the apps, as a first step you'll need to:
153153
154154
1. All APIs must publish a minimum of one [scope](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-auth-code-flow#request-an-authorization-code), also called [Delegated Permission](https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#permission-types), for the client's to obtain an access token for a *user* successfully. To publish a scope, follow these steps:
155155
1. Select **Add a scope** button open the **Add a scope** screen and Enter the values as indicated below:
156-
1. For **Scope name**, use `TodoList.Read`.
156+
1. For **Scope name**, enter **TodoList.Read** (case-sensitive).
157157
1. Select **Admins and users** options for **Who can consent?**.
158-
1. For **Admin consent display name** type in the details, `e.g. Allow the users of the app msal-dotnet-api to read ToDo list items`.
159-
1. For **Admin consent description** type in the details `e.g. Allows the app msal-dotnet-api to read the signed-in users ToDo list items.`
160-
1. For **User consent display name** type in the details `e.g. Read ToDo list items as yourself`.
161-
1. For **User consent description** type in the details `e.g. Allow the app msal-dotnet-api to read ToDo list items on your behalf.`
158+
1. For **Admin consent display name** type in the details, `e.g. Allow the users of the app msal-dotnet-api to read Todolist items`.
159+
1. For **Admin consent description** type in the details `e.g. Allows the app msal-dotnet-api to read the signed-in users Todolist items.`
160+
1. For **User consent display name** type in the details `e.g. Read Todolist items as yourself`.
161+
1. For **User consent description** type in the details `e.g. Allow the app msal-dotnet-api to read Todolist items on your behalf.`
162162
1. Keep **State** as **Enabled**.
163163
1. Select the **Add scope** button on the bottom to save this scope.
164164
> Repeat the steps above for another scope named **TodoList.ReadWrite**
165165
1. Select the **Manifest** blade on the left.
166166
1. Set `accessTokenAcceptedVersion` property to **2**.
167167
1. Select on **Save**.
168168
169-
> :information_source: Follow [the principle of least privilege](https://docs.microsoft.com/azure/active-directory/develop/secure-least-privileged-access) whenever you are publishing permissions for a web API.
169+
> :information_source: Follow [the principle of least privilege](https://docs.microsoft.com/azure/active-directory/develop/secure-least-privileged-access) whenever you are publishing permissions for a web API.
170170
171171
##### Publish Application Permissions
172172
@@ -175,7 +175,7 @@ To manually register the apps, as a first step you'll need to:
175175
1. Select **Create app role**:
176176
1. For **Display name**, enter a suitable name for your application permission, for instance **TodoList.Read.All**.
177177
1. For **Allowed member types**, choose **Application** to ensure other applications can be granted this permission.
178-
1. For **Value**, enter **TodoList.Read.All**.
178+
1. For **Value**, enter **TodoList.Read.All** (case-sensitive).
179179
1. For **Description**, enter **Allow this application to read every users Todo list items**.
180180
1. Select **Apply** to save your changes.
181181
> Repeat the steps above for another app permission named **TodoList.ReadWrite.All**
@@ -210,6 +210,12 @@ Open the project in your IDE (like Visual Studio or Visual Studio Code) to confi
210210
1. Under **Supported account types**, select **Accounts in this organizational directory only**
211211
1. Select **Register** to create the application.
212212
1. In the **Overview** blade, find and note the **Application (client) ID**. You use this value in your app's configuration file(s) later in your code.
213+
1. In the app's registration screen, select the **Authentication** blade to the left.
214+
1. If you don't have a platform added, select **Add a platform** and select the **Single-page application** option.
215+
1. In the **Redirect URI** section enter the following redirect URIs:
216+
1. `http://localhost:4200/`
217+
1. `http://localhost:4200/auth`
218+
1. Click **Save** to save your changes.
213219
1. Since this app signs-in users, we will now proceed to select **delegated permissions**, which is is required by apps signing-in users.
214220
1. In the app's registration screen, select the **API permissions** blade in the left to open the page where we add access to the APIs that your application needs:
215221
1. Select the **Add a permission** button and then,

3-Authorization-II/1-call-api/SPA/src/app/app-routing.module.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NgModule } from '@angular/core';
22
import { Routes, RouterModule } from '@angular/router';
3-
import { MsalGuard } from '@azure/msal-angular';
3+
import { MsalGuard, MsalRedirectComponent } from '@azure/msal-angular';
44
import { BrowserUtils } from '@azure/msal-browser';
55

66
import { HomeComponent } from './home/home.component';
@@ -27,19 +27,9 @@ const routes: Routes = [
2727
]
2828
},
2929
{
30-
// Needed for hash routing
31-
path: 'error',
32-
component: HomeComponent
33-
},
34-
{
35-
// Needed for hash routing
36-
path: 'state',
37-
component: HomeComponent
38-
},
39-
{
40-
// Needed for hash routing
41-
path: 'code',
42-
component: HomeComponent
30+
// Needed for handling redirect after login
31+
path: 'auth',
32+
component: MsalRedirectComponent
4333
},
4434
{
4535
path: '',

3-Authorization-II/1-call-api/SPA/src/app/app.module.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
2424
import { IPublicClientApplication, PublicClientApplication, InteractionType } from '@azure/msal-browser';
2525
import {
2626
MsalGuard, MsalInterceptor, MsalBroadcastService, MsalInterceptorConfiguration, MsalModule, MsalService,
27-
MSAL_GUARD_CONFIG, MSAL_INSTANCE, MSAL_INTERCEPTOR_CONFIG, MsalGuardConfiguration, MsalRedirectComponent
27+
MSAL_GUARD_CONFIG, MSAL_INSTANCE, MSAL_INTERCEPTOR_CONFIG, MsalGuardConfiguration, MsalRedirectComponent, ProtectedResourceScopes
2828
} from '@azure/msal-angular';
2929

3030
import { msalConfig, loginRequest, protectedResources } from './auth-config';
@@ -43,10 +43,26 @@ export function MSALInstanceFactory(): IPublicClientApplication {
4343
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-angular/docs/v2-docs/initialization.md#get-tokens-for-web-api-calls
4444
*/
4545
export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
46-
const protectedResourceMap = new Map<string, Array<string>>();
46+
const protectedResourceMap = new Map<string, Array<string | ProtectedResourceScopes> | null>();
4747

48-
protectedResourceMap.set(protectedResources.apiTodoList.endpoint, protectedResources.apiTodoList.scopes.read);
49-
protectedResourceMap.set(protectedResources.apiTodoList.endpoint, protectedResources.apiTodoList.scopes.write);
48+
protectedResourceMap.set(protectedResources.apiTodoList.endpoint, [
49+
{
50+
httpMethod: 'GET',
51+
scopes: [...protectedResources.apiTodoList.scopes.read]
52+
},
53+
{
54+
httpMethod: 'POST',
55+
scopes: [...protectedResources.apiTodoList.scopes.write]
56+
},
57+
{
58+
httpMethod: 'PUT',
59+
scopes: [...protectedResources.apiTodoList.scopes.write]
60+
},
61+
{
62+
httpMethod: 'DELETE',
63+
scopes: [...protectedResources.apiTodoList.scopes.write]
64+
}
65+
]);
5066

5167
return {
5268
interactionType: InteractionType.Popup,

0 commit comments

Comments
 (0)