简体   繁体   English

使用B2C在Javascript / Angular 6 SPA应用程序中通过MSAL.JS注销的错误

[英]Error in Logout through MSAL.JS in Javascript / Angular 6 SPA application using B2C

I have one Javascript SPA application using MSAL.JS for authentication against Azure AD B2C and another Angular 6 SPA application using MSAL for Angular against Azure AD B2C. 我有一个使用MSAL.JS针对Azure AD B2C进行身份验证的Javascript SPA应用程序,还有一个使用MSAL针对Azure AD B2C用于Angular身份验证的Angular 6 SPA应用程序。

In both the applications the logout is throwing below error. 在这两个应用程序中,注销均抛出错误。

Correlation ID: 6de6e068-7b07-4d24-bac4-c1af3131815b Timestamp: 2018-09-25 16:16:20Z AADB2C90272: The id_token_hint parameter has not been specified in the request. 相关ID:6de6e068-7b07-4d24-bac4-c1af3131815b时间戳记:2018-09-25 16:16:20Z AADB2C90272:在请求中未指定id_token_hint参数。 Please provide token and try again. 请提供令牌,然后重试。

For Logout, MSAL has very simple logout api which does not take any parameter, so how can I provide id_token_hint? 对于注销,MSAL具有非常简单的注销api,它不带任何参数,因此如何提供id_token_hint? Am I missing something? 我想念什么吗? Is there any config parameter I need to provide while injecting MsalModule in Angular Application. 在Angular Application中注入MsalModule时是否需要提供任何配置参数。 Or anything similar in Javascript app for Msal.UserAgentApplication. 或Msal.UserAgentApplication的Javascript应用中类似的内容。

I m basically using the currently latest "msal": "^0.2.3" , this is my authentication service, there is no configuration needed in the app.module, and the logout works perfectly: 我基本上使用的是当前最新的“ msal”:“ ^ 0.2.3”,这是我的身份验证服务,在app.module中不需要配置,并且注销可以正常工作:

import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';
import * as Msal from 'msal';
import { User } from "msal/lib-commonjs/User";
import { ApiService } from './api.service';
import { BackendRoutes } from './backend.routes';

@Injectable()
export class AuthenticationService {
    private _clientApplication: Msal.UserAgentApplication;
    private _authority: string;

constructor(private apiService: ApiService, private backendRoutes: BackendRoutes) {
    this._authority = `https://login.microsoftonline.com/tfp/${environment.tenant}/${environment.signUpSignInPolicy}`;

    this._clientApplication =
        new Msal.UserAgentApplication(
            environment.clientID,
            this._authority,
            this.msalHandler,
            {
                cacheLocation: 'localStorage',
                redirectUri: window.location.origin
            });
}

msalHandler(errorDesc: any, token: any, error: any, tokenType: any) {
    let userAgent: Msal.UserAgentApplication = <any>(this);
    if (errorDesc.indexOf("AADB2C90118") > -1) {
        //Forgotten password
        userAgent.authority = `https://login.microsoftonline.com/tfp/${environment.tenant}/${environment.passResetPolicy}`;
        userAgent.loginRedirect(environment.b2cScopes);

    } else if (errorDesc.indexOf("AADB2C90077") > -1) {
        //Expired Token, function call from interceptor with proper context
        this.logout();
    }
}

addUser(): void {
    if (this.isOnline()) {
        this.apiService.post(this.backendRoutes.addUser).subscribe();
    }
}

login(): void {
    this._clientApplication.loginRedirect(environment.b2cScopes);
}

logout(): void {
    this._clientApplication.logout();
}

getAuthenticationToken(): Promise<string> {
    return this._clientApplication.acquireTokenSilent(environment.b2cScopes)
        .then(token => token)
        .catch(error => {
            return Promise.reject(error);
        });
}

And the interceptor linked to it: 和拦截器链接到它:

export class AuthenticationHttpInterceptor implements HttpInterceptor {

    constructor(private authenticationService: AuthenticationService) {
    }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return from(this.authenticationService.getAuthenticationToken()
            .then(token => {
                return req.clone({
                    setHeaders: {
                        Authorization: `Bearer ${token}`
                    }
                });
            })
            .catch(err => {
                this.authenticationService.msalHandler(err,null,null,null);
                return req;
            }))
            .switchMap(req => {
                return next.handle(req);
            });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM