我正在开发一个使用AD B2C作为OpenID连接提供者的云应用程序。
在我的配置环境的下面:
AD B2C
在我的AD B2C中,我创建了两个应用程序:
API网关
我创建了一个API管理。在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>MyClient
我的客户是一个角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直接访问,我可以成功地调用相同的API。

为什么我的API管理器不授权我的应用程序?
非常感谢
发布于 2018-03-31 00:09:50
我认为发生上述错误是因为API网关不承认从角客户端传递的访问令牌的aud (受众)声明。
上面的场景类似于"Azure AD B2C:从.NET web应用程序调用.NET web API“,其中角客户端是web应用程序,API网关是web。
我建议你:
1)表示API网关的创建一个Azure AD B2C应用程序。输入该应用程序的API以标识API网关。
2)该网关应用程序的添加一个或多个权限范围和/或保留user_impersonation的默认权限范围。
3)表示角客户端的创建一个Azure AD B2C应用程序。您已经创建了这个客户端应用程序。
4) 允许客户端应用程序访问网关应用程序,使客户端应用程序能够获得访问令牌,以代表登录用户调用网关应用程序。
5)具有网关应用程序ID的政策。
<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/tfp/{tenant}/{policy}/v2.0/.well-known/openid-configuration" />
<audiences>
<audience><!-- Paste the app ID for the gateway app --></audience>
</audiences>
</validate-jwt>6)包括步骤2中添加到客户端应用程序到tenantConfig.b2cScopes数组的任何权限范围。
发布于 2018-03-30 15:04:50
基于
{ "statusCode":401,“消息”:“未经授权。访问令牌丢失或无效”}
当您发出http请求时,似乎没有从角应用程序发送令牌。一种在角度上这样做的方法是使用拦截器。
@Injectable()
export class AuthenticationHttpInterceptor implements HttpInterceptor {
constructor(private authenticationService: AuthenticationService) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return Observable.fromPromise(this.authenticationService.getAuthenticationToken())
.switchMap(token => {
req = req.clone({
setHeaders: {
Authorization: `Bearer ${token}`
}
});
return next.handle(req);
});
}
}https://stackoverflow.com/questions/49576617
复制相似问题