【发布时间】:2018-03-30 14:54:14
【问题描述】:
我正在开发一个使用 AD B2C 作为 OpenID Connect Provider 的云应用程序。
在我的配置环境下:
广告 B2C
在我的 AD B2C 中,我创建了两个应用程序:
- DeveloperPortal - 用于在开发者门户中配置授权。
- MyClient - 用于在内部配置授权。
API 网关
我创建了一个API Management。在 API 导入之后,我添加了如下一项策略:
<validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid.">
<openid-config url="https://login.microsoftonline.com/{myTenantAzureId}/.well-known/openid-configuration?p={myPolicies}" />
</validate-jwt>
我的客户
我的客户端是一个 Angular 4 应用程序。我正在使用MSAL.js微软官方库。
这是我的授权服务 TypeScript 类:
import { Injectable } from '@angular/core';
declare var bootbox: any;
declare var Msal: any;
@Injectable()
export class MsalService {
public access_token: string;
private logger = new Msal.Logger(this.loggerCallback, { level: Msal.LogLevel.Verbose });
tenantConfig = {
tenant: "{myTenant}.onmicrosoft.com",
clientID: '{MyClientClientId}',
signUpSignInPolicy: "{myPolicies}",
b2cScopes: ["openid"]
};
options = {
logger: this.logger,
postLogoutRedirectUri: window.location.protocol + "//" + window.location.host
}
//authority = null;
authority = "https://login.microsoftonline.com/tfp/" + this.tenantConfig.tenant + "/" + this.tenantConfig.signUpSignInPolicy;
clientApplication = new Msal.UserAgentApplication(
this.tenantConfig.clientID,
this.authority,
this.authCallback,
this.options
);
public login(callback: Function): void {
var _this = this;
this.clientApplication.loginPopup(this.tenantConfig.b2cScopes).then(function (idToken: any) {
_this.clientApplication.acquireTokenSilent(_this.tenantConfig.b2cScopes).then(
function (accessToken: any) {
_this.access_token = accessToken;
localStorage.setItem("access_token", accessToken);
if (callback) {
callback(accessToken);
}
}, function (error: any) {
_this.clientApplication.acquireTokenPopup(_this.tenantConfig.b2cScopes).then(
function (accessToken: any) {
_this.access_token = accessToken;
console.log(accessToken);
}, function (error: any) {
bootbox.alert("Error acquiring the popup:\n" + error);
});
})
}, function (error: any) {
console.log(error);
bootbox.alert("Error during login:\n" + error);
});
}
public logout(callback: Function): void {
this.clientApplication.logout();
if (callback) {
callback();
}
}
private loggerCallback(logLevel, message, piiLoggingEnabled) {
console.log(message);
}
private authCallback(errorDesc: any, token: any, error: any, tokenType: any) {
if (token) {
}
else {
console.error(error + ":" + errorDesc);
}
}
}
问题
如果我尝试使用带有访问令牌的授权标头调用 API 管理的 API,我会收到此错误:
{ "statusCode": 401, "message": "Unauthorized. Access token is missing or invalid." }
但如果我尝试通过 Developer Portal 直接访问,我可以成功调用相同的 API。
为什么我的 API Manager 不授权我的应用程序?
非常感谢
【问题讨论】:
-
您是否将令牌添加到您的 http 请求中?例如:authentication.httpInterceptor.ts
-
嗨,spotmahn,我也尝试过使用邮递员,并使用浏览器检查了请求。
标签: angular typescript azure-ad-b2c azure-api-management