Skip to content

Commit 1902bb3

Browse files
committed
update readme
1 parent ba3b019 commit 1902bb3

File tree

2 files changed

+22
-73
lines changed

2 files changed

+22
-73
lines changed

.github/workflows/dotnet.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ jobs:
1818
with:
1919
dotnet-version: 3.1.x
2020

21-
- run: |
22-
cd 3-Authorization-II/2-call-api-b2c/API
23-
dotnet restore
24-
dotnet build --no-restore
25-
cd TodoListAPI.Tests
26-
dotnet test --no-build --verbosity normal
27-
2821
- run: |
2922
cd 6-AdvancedScenarios/1-call-api-obo/API
3023
dotnet restore
@@ -50,6 +43,13 @@ jobs:
5043
cd TodoListAPI.Tests
5144
dotnet test --no-build --verbosity normal
5245
46+
- run: |
47+
cd 3-Authorization-II/2-call-api-b2c/API
48+
dotnet restore
49+
dotnet build --no-restore
50+
cd TodoListAPI.Tests
51+
dotnet test --no-build --verbosity normal
52+
5353
- run: |
5454
cd 5-AccessControl/1-call-api-roles/API
5555
dotnet restore

3-Authorization-II/2-call-api-b2c/README.md

Lines changed: 15 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -262,78 +262,27 @@ For validation and debugging purposes, developers can decode **JWT**s (*JSON Web
262262

263263
### Verifying permissions
264264

265-
Access tokens that have neither the **scp** (for delegated permissions) nor **roles** (for application permissions) claim with the required scopes/permissions should not be accepted. In the sample, this is illustrated via the `RequiredScopeOrAppPermission` attribute in [TodoListController.cs](./API/TodoListAPI/Controllers/TodoListController.cs):
266-
267-
```csharp
268-
[HttpGet]
269-
/// <summary>
270-
/// An access token issued by Azure AD will have at least one of the two claims. Access tokens
271-
/// issued to a user will have the 'scp' claim. Access tokens issued to an application will have
272-
/// the roles claim. Access tokens that contain both claims are issued only to users, where the scp
273-
/// claim designates the delegated permissions, while the roles claim designates the user's role.
274-
/// </summary>
275-
[RequiredScopeOrAppPermission(
276-
AcceptedScope = new string[] { _todoListRead, _todoListReadWrite },
277-
AcceptedAppPermission = new string[] { _todoListReadAll, _todoListReadWriteAll }
278-
)]
279-
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
280-
{
281-
// route logic ...
282-
}
283-
```
284-
285-
### Access to data
286-
287-
Web API endpoints should be prepared to accept calls from both users and applications, and should have control structures in place to respond to each accordingly. For instance, a call from a user via delegated permissions should be responded with user's data, while a call from an application via application permissions might be responded with the entire todolist. This is illustrated in the [TodoListController](./API/TodoListAPI/Controllers/TodoListController.cs) controller:
265+
Access tokens that does not have the **scp** (for delegated permissions) claim with the required scopes/permissions should not be accepted. In the sample, this is illustrated via the `RequiredScope` attribute in [TodoListController.cs](./API/TodoListAPI/Controllers/TodoListController.cs):
288266

289267
```csharp
290268
// GET: api/TodoItems
291269
[HttpGet]
292-
[RequiredScopeOrAppPermission(
293-
AcceptedScope = new string[] { _todoListRead, _todoListReadWrite },
294-
AcceptedAppPermission = new string[] { _todoListReadAll, _todoListReadWriteAll }
295-
)]
270+
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes:Read")]
296271
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
297272
{
298-
if (!IsAppOnlyToken())
299-
{
300-
/// <summary>
301-
/// The 'oid' (object id) is the only claim that should be used to uniquely identify
302-
/// a user in an Azure AD tenant. The token might have one or more of the following claim,
303-
/// that might seem like a unique identifier, but is not and should not be used as such:
304-
///
305-
/// - upn (user principal name): might be unique amongst the active set of users in a tenant
306-
/// but tend to get reassigned to new employees as employees leave the organization and others
307-
/// take their place or might change to reflect a personal change like marriage.
308-
///
309-
/// - email: might be unique amongst the active set of users in a tenant but tend to get reassigned
310-
/// to new employees as employees leave the organization and others take their place.
311-
/// </summary>
312-
return await _context.TodoItems.Where(x => x.Owner == HttpContext.User.GetObjectId()).ToListAsync();
313-
}
314-
else
315-
{
316-
return await _context.TodoItems.ToListAsync();
317-
}
318-
}
319-
320-
/// <summary>
321-
/// Indicates if the AT presented has application or delegated permissions.
322-
/// </summary>
323-
/// <returns></returns>
324-
private bool IsAppOnlyToken()
325-
{
326-
// Add in the optional 'idtyp' claim to check if the access token is coming from an application or user.
327-
// See: https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-optional-claims
328-
if (HttpContext.User.Claims.Any(c => c.Type == "idtyp"))
329-
{
330-
return HttpContext.User.Claims.Any(c => c.Type == "idtyp" && c.Value == "app");
331-
}
332-
else
333-
{
334-
// alternatively, if an AT contains the roles claim but no scp claim, that indicates it's an app token
335-
return HttpContext.User.Claims.Any(c => c.Type == "roles") && HttpContext.User.Claims.Any(c => c.Type != "scp");
336-
}
273+
/// <summary>
274+
/// The 'oid' (object id) is the only claim that should be used to uniquely identify
275+
/// a user in an Azure AD tenant. The token might have one or more of the following claim,
276+
/// that might seem like a unique identifier, but is not and should not be used as such:
277+
///
278+
/// - upn (user principal name): might be unique amongst the active set of users in a tenant
279+
/// but tend to get reassigned to new employees as employees leave the organization and others
280+
/// take their place or might change to reflect a personal change like marriage.
281+
///
282+
/// - email: might be unique amongst the active set of users in a tenant but tend to get reassigned
283+
/// to new employees as employees leave the organization and others take their place.
284+
/// </summary>
285+
return await _TodoListContext.TodoItems.Where(x => x.Owner == _currentPrincipalId).ToListAsync();
337286
}
338287
```
339288

0 commit comments

Comments
 (0)