本教學課程是一系列的第 3 部分,示範如何建置 Angular 單頁應用程式 (SPA),該應用程式會使用 Microsoft 身分識別平台 進行驗證。 在本教學課程中,您會將登入和註銷體驗新增至 Angular SPA。
開啟 檔案, src/app/app.component.html
並以下列程式代碼取代內容。
<a class="navbar navbar-dark bg-primary" variant="dark" href="/">
<a class="navbar-brand"> Microsoft Identity Platform </a>
<a>
<button *ngIf="!loginDisplay" class="btn btn-secondary" (click)="login()">Sign In</button>
<button *ngIf="loginDisplay" class="btn btn-secondary" (click)="logout()">Sign Out</button>
</a>
</a>
<a class="profileButton">
<a [routerLink]="['profile']" class="btn btn-secondary" *ngIf="loginDisplay">View Profile</a>
</a>
<div class="container">
<router-outlet></router-outlet>
</div>
程序代碼會在 Angular 應用程式中實作導覽列。 它會根據使用者驗證狀態動態顯示 [登入 ] 和 [註銷 ] 按鈕,並包含 登入使用者的 [檢視配置檔 ] 按鈕,以增強應用程式的使用者介面。 login()
當選取按鈕時,會呼叫 中的 src/app/app.component.ts
和 logout()
方法。
開啟 檔案, src/app/app-routing.module.ts
並以下列程式代碼取代內容。
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ProfileComponent } from './profile/profile.component';
import { HomeComponent } from './home/home.component';
import { MsalGuard } from '@azure/msal-angular';
const routes: Routes = [
{
path: 'profile',
component: ProfileComponent,
canActivate: [
MsalGuard
]
},
{
path: '**',
component: HomeComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes, {
useHash: true
})],
exports: [RouterModule]
})
export class AppRoutingModule { }
代碼段會藉由建立 Profile 和 Home 元件的路徑,在 Angular 應用程式中設定路由。 它會使用 MsalGuard
在配置檔路由上強制執行驗證,而所有不相符的路徑都會重新導向至 Home 元件。
開啟 檔案, src/app/home/home.component.ts
並以下列程式代碼取代內容。
import { Component, OnInit } from '@angular/core';
import { MsalBroadcastService, MsalService } from '@azure/msal-angular';
import { EventMessage, EventType, AuthenticationResult } from '@azure/msal-browser';
import { filter } from 'rxjs/operators';
@Component({
selector: 'app-home',
templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {
constructor(
private authService: MsalService,
private msalBroadcastService: MsalBroadcastService
) { }
ngOnInit(): void {
this.msalBroadcastService.msalSubject$
.pipe(
filter((msg: EventMessage) => msg.eventType === EventType.LOGIN_SUCCESS),
)
.subscribe((result: EventMessage) => {
const payload = result.payload as AuthenticationResult;
this.authService.instance.setActiveAccount(payload.account);
});
}
}
程式代碼會設定名為 HomeComponent
的 Angular 元件,該元件會與 Microsoft 驗證連結庫 (MSAL) 整合。 在ngOnInit
生命周期攔截中,元件會從訂閱可MsalBroadcastService
觀察的 msalSubject$
,篩選登入成功事件。 當登入事件發生時,它會擷取驗證結果,並在 中設定使用中的 MsalService
帳戶,讓應用程式能夠管理用戶會話。
開啟 檔案, src/app/home/home.component.html
並以下列程式代碼取代內容。
<div class="title">
<h5>
Welcome to the Microsoft Authentication Library For Javascript - Angular SPA
</h5>
<p >View your data from Microsoft Graph by clicking the "View Profile" link above.</p>
</div>
此程式代碼會歡迎使用者前往應用程式,並提示他們按兩下 [檢視配置檔 ] 連結來檢視其Microsoft圖形數據。
開啟 檔案, src/main.ts
並以下列程式代碼取代內容。
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
代碼段會 platformBrowserDynamic
從 Angular 的平台瀏覽器動態模組和 AppModule
應用程式的模組檔案匯入。 然後,它會使用 platformBrowserDynamic()
來啟動 AppModule
,初始化 Angular 應用程式。 啟動程式程式期間發生的任何錯誤都會攔截並記錄到主控台。
開啟 檔案, src/index.html
並以下列程式代碼取代內容。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MSAL For Javascript - Angular SPA</title>
</head>
<body>
<app-root></app-root>
<app-redirect></app-redirect>
</body>
</html>
代碼段會定義具有英文的 HTML5 檔做為語言和 UTF-8 字元編碼。 它會將標題設定為 「MSAL For Javascript - Angular SPA」。。本文包含 <app-root>
元件作為主要進入點,以及 <app-redirect>
重新導向功能的元件。
開啟 檔案, src/styles.css
並以下列程式代碼取代內容。
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
.app {
text-align: center;
padding: 8px;
}
.title{
text-align: center;
padding: 18px;
}
.profile{
text-align: center;
padding: 18px;
}
.profileButton{
display: flex;
justify-content: center;
padding: 18px;
}
CSS 程式代碼會將本文字型設定為新式 sans-serif 堆棧、移除默認邊界,以及套用字型平滑,以增強可讀性來設定網頁樣式。 它會將文字置中 .app
,並將邊框間距新增至、 .title
和 .profile
類別,而 類別 .profileButton
則使用 flexbox 將元素置中。