0

angularプロジェクトでoidc-clientを使用しています。ユーザー オブジェクトが null でない場合にユーザーをホームページにリダイレクトするために、app.component.ts に以下のコードを記述しました。ただし、ログインに成功した後でも、ユーザー オブジェクトは常に null です。認証に Microsoft Azure を使用しています。

app.component.ts

async ngOnInit() {
    await this.authService.getUser().then(user => {
        console.log(user) // always returns null even after login
        if (user) {
            this.router.navigate(['/home']);
        }
    });
}

login() {
    this.authService.signinRedirect();
}

AuthenticationService.ts

export class AuthenticationService {
    
    private userManager: UserManager;
    constructor(settings: Settings) {
        this.userManager = new UserManager(settings);
    }

    public signinRedirect() {
       this.userManager.signinRedirect();
    }

    public getUser() {
        return this.userManager.getUser();
    }
}

ログインに成功した後、oidc.user.xxxxxxx という名前のローカル/セッション ストレージに key:val ペアが表示されません。

また、いくつかの社内アプリケーションのサンプルをクロスチェックしました。ログインが成功すると、oidc api はトークン エンドポイントへの呼び出しをトリガーします。私の場合、このエンドポイントは呼び出されていません。

何が欠けている可能性がありますか?私は何を間違っていますか?

4

1 に答える 1

0

app.component.ts にサインイン リダイレクト コールバックを登録すると、この問題は解決しました。

async ngOnInit() {
if (window.location.href.includes('code')) { // without this if condition I am getting an error saying no state in response. not sure if there is a better way.
     this.authService.signinRedirectCallback();
}

await this.authService.getUser().then(user => {
    console.log(user) // always returns null even after login
    if (user) {
        this.router.navigate(['/home']);
    }
});

}

于 2021-06-03T17:12:32.563 に答える