Skip to content

Commit 8ff002f

Browse files
committed
review changes
1 parent 93dcb71 commit 8ff002f

File tree

8 files changed

+60
-9
lines changed

8 files changed

+60
-9
lines changed

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.EntityFrameworkCore;
88
using Microsoft.Identity.Web;
9-
using System.IdentityModel.Tokens.Jwt;
9+
using Microsoft.AspNetCore.Authentication.JwtBearer;
1010

1111
using TodoListAPI.Models;
1212

@@ -25,7 +25,36 @@ public Startup(IConfiguration configuration)
2525
public void ConfigureServices(IServiceCollection services)
2626
{
2727
// Adds Microsoft Identity platform (AAD v2.0) support to protect this Api
28-
services.AddMicrosoftIdentityWebApiAuthentication(Configuration);
28+
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
29+
.AddMicrosoftIdentityWebApi(options =>
30+
{
31+
Configuration.Bind("AzureAd", options);
32+
options.Events = new JwtBearerEvents();
33+
34+
/// <summary>
35+
/// Below you can do extended token validation and check for additional claims, such as:
36+
///
37+
/// - check if the caller's tenant is in the allowed tenants list via the 'tid' claim (for multi-tenant applications)
38+
/// - check if the caller's account is homed or guest via the 'acct' optional claim
39+
/// - check if the caller belongs to right roles or groups via the 'roles' or 'groups' claim, respectively
40+
///
41+
/// Bear in mind that you can do any of the above checks within the individual routes and/or controllers as well.
42+
/// For more information, visit: https://docs.microsoft.com/azure/active-directory/develop/access-tokens#validate-the-user-has-permission-to-access-this-data
43+
/// </summary>
44+
options.Events.OnTokenValidated = async context =>
45+
{
46+
// Uncomment the lines below to validate the caller's tenant ID.
47+
48+
// string[] allowedTenants = {/* add a list of tenant IDs */ };
49+
// string tenantId = context.Principal.Claims.FirstOrDefault(x => x.Type == "tid")?.Value;
50+
51+
// if (!allowedTenants.Contains(tenantId))
52+
// {
53+
// throw new Exception("This tenant is not authorized");
54+
// }
55+
56+
};
57+
}, options => { Configuration.Bind("AzureAd", options); });
2958

3059
services.AddDbContext<TodoContext>(opt => opt.UseInMemoryDatabase("TodoList"));
3160

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const routes: Routes = [
2727
MsalGuard
2828
]
2929
},
30+
{
31+
path: 'redirect',
32+
component: TodoViewComponent,
33+
},
3034
{
3135
// Needed for hash routing
3236
path: 'error',

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { MatFormFieldModule } from '@angular/material/form-field'
1717
import { AppRoutingModule } from './app-routing.module';
1818
import { AppComponent } from './app.component';
1919
import { HomeComponent } from './home/home.component';
20+
import { RedirectComponent } from './redirect/redirect.component';
2021
import { TodoEditComponent } from './todo-edit/todo-edit.component';
2122
import { TodoViewComponent } from './todo-view/todo-view.component';
2223
import { TodoService } from './todo.service';
@@ -29,7 +30,6 @@ import {
2930
} from '@azure/msal-angular';
3031

3132
import { msalConfig, loginRequest, protectedResources } from './auth-config';
32-
3333
/**
3434
* Here we pass the configuration parameters to create an MSAL instance.
3535
* For more info, visit: https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-angular/docs/v2-docs/configuration.md
@@ -62,7 +62,7 @@ export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
6262
*/
6363
export function MSALGuardConfigFactory(): MsalGuardConfiguration {
6464
return {
65-
interactionType: InteractionType.Redirect,
65+
interactionType: InteractionType.Popup,
6666
authRequest: loginRequest
6767
};
6868
}
@@ -72,7 +72,8 @@ export function MSALGuardConfigFactory(): MsalGuardConfiguration {
7272
AppComponent,
7373
HomeComponent,
7474
TodoViewComponent,
75-
TodoEditComponent
75+
TodoEditComponent,
76+
RedirectComponent
7677
],
7778
imports: [
7879
BrowserModule,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const msalConfig: Configuration = {
1818
auth: {
1919
clientId: 'Enter_the_Application_Id_Here', // This is the ONLY mandatory field that you need to supply.
2020
authority: 'https://login.microsoftonline.com/Enter_the_Tenant_Info_Here', // Defaults to "https://login.microsoftonline.com/common"
21-
redirectUri: '/', // Points to window.location.origin. You must register this URI on Azure portal/App Registration.
21+
redirectUri: '/redirect', // Points to window.location.origin. You must register this URI on Azure portal/App Registration.
2222
clientCapabilities: ['CP1'] // This lets the resource server know that this client can handle claim challenges.
2323
},
2424
cache: {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export class HomeComponent implements OnInit {
2525
filter((msg: EventMessage) => msg.eventType === EventType.LOGIN_SUCCESS),
2626
)
2727
.subscribe((result: EventMessage) => {
28-
console.log(result);
2928
const payload = result.payload as AuthenticationResult;
3029
this.authService.instance.setActiveAccount(payload.account);
3130
});
@@ -45,7 +44,9 @@ export class HomeComponent implements OnInit {
4544
}
4645

4746
getClaims(claims: any) {
48-
const claimsTable = createClaimsTable(claims);
49-
this.dataSource = [...claimsTable];
47+
if (claims) {
48+
const claimsTable = createClaimsTable(claims);
49+
this.dataSource = [...claimsTable];
50+
}
5051
}
5152
}

3-Authorization-II/1-call-api/SPA/src/app/redirect/redirect.component.css

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>This page is left blank intentionally</p>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Component, OnInit } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-redirect',
5+
templateUrl: './redirect.component.html',
6+
styleUrls: ['./redirect.component.css']
7+
})
8+
export class RedirectComponent implements OnInit {
9+
10+
constructor() { }
11+
12+
ngOnInit(): void {
13+
}
14+
15+
}

0 commit comments

Comments
 (0)