gpt4 book ai didi

Angular 客户端需要 Web API 的访问 token

转载 作者:行者123 更新时间:2023-12-03 04:20:47 24 4
gpt4 key购买 nike

我有一个受 OWIN 保护的 Web API,它要求使用 Azure Active Directory 对用户进行身份验证。我可以使用通过 Microsoft 授权 URL 获得的访问 token 通过 Postman 发出请求。

但是,从我的 Angular 客户端(也由 Azure AD 保护)中,我无法在不重定向到 API 的情况下获取 Web API 的访问 token 。客户端应用程序(其客户端 ID 与服务器 API 不同)是否有任何方法可以检索它需要在 API 请求的授权 header 中使用的访问 token ?

最佳答案

您已在 Azure Active Directory 中注册了两个应用程序(您的 UI 和 API)。您需要代表当前用户授予API的UI访问权限。您使用注册的 UI 应用程序 ID 登录用户,并最终检索 API 的访问 token 。

这里是 ng2-adal 组件的伪代码:

import { Injectable } from '@angular/core';

@Injectable()
export class AuthconfigService {

constructor() { }

public get adalConfig(): any {
return {
tenant: 'yourTenant.onmicrosoft.com',
clientId: 'UI AppId (GUID)',
redirectUri: window.location.origin + '/',
postLogoutRedirectUri: window.location.origin + '/'
};
}

public get apiKey(): string
{
return "API AppId (GUID)";
}

}

然后在你的app.component.ts中:

this.adalService.init(this.authconfigService.adalConfig);
this.adalService.login();

this.adalService.acquireToken(this.authconfigService.apiKey).subscribe(
(data) => {
.....});

关于Angular 客户端需要 Web API 的访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49265567/

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