gpt4 book ai didi

angular - 如何将我的 Angular 应用程序与 Azure Active Directory 绑定(bind)?

转载 作者:行者123 更新时间:2023-12-03 03:59:05 25 4
gpt4 key购买 nike

我创建了一个只有两个组件的 Angular 应用程序:登录组件和用户详细信息组件。只想显示登录用户的详细信息。每当我单击应用程序中的登录按钮时,它会将我重定向到 azure-client。在浏览器上获取以下重定向网址。请建议/推荐后续步骤?

enter image description here

app.module.ts

@NgModule({
declarations: [
LoginComponent,
UserDetailComponent,
AppComponent
],
imports: [
BrowserModule,
FormsModule,
RouterModule,
HttpClientModule,
ReactiveFormsModule,
AppRoutingModule,
MsalModule.forRoot({
auth: {
clientId: '************************************', // This is your client ID
authority: '********************************************', // This is your tenant info
redirectUri: 'http://localhost:4200/' // This is your redirect URI
},
cache: {
cacheLocation: 'localStorage'
},
}),
],
providers: [UserService,
{
provide: HTTP_INTERCEPTORS,
useClass: MsalInterceptor,
multi: true
}],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
bootstrap: [AppComponent]
})
export class AppModule { }

app.routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { UserDetailComponent } from './user-detail/user-detail.component';

const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'login', component: LoginComponent },
{ path: 'userDetail', component: UserDetailComponent },
{ path: '**', redirectTo: '' }
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

单击自定义 Ui 的登录按钮时将调用的登录方法

login(username, password) : Observable<any> {

let headers: HttpHeaders = new HttpHeaders();

headers = headers.append('Content-Type', 'application/x-www-form-urlencoded');
headers = headers.append('Content-type', 'application/json');
headers = headers.append('Content-type', 'text/html');
headers = headers.append('Access-Control-Allow-Origin', '*');
headers = headers.append('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
headers = headers.append('Access-Control-Allow-Headers', '*');
headers = headers.append('Access-Control-Allow-Credentials', 'true');

return this.http.post<any>('http://localhost:8082/check', { username, password }, {headers})
.pipe(map(user => {
return user;
}));
}

最佳答案

要使用 MsalModule 登录 Azure,您必须调用

msalService.loginPopup();

 msalService.loginRedirect();

前者是建议的。

参见https://github.com/Azure-Samples/active-directory-javascript-singlepageapp-angular/blob/master/src/app/app.component.ts

关于angular - 如何将我的 Angular 应用程序与 Azure Active Directory 绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63258585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com