2017-09-26 17 views
1

Azure AD B2Cを使用してAngular 2+アプリの認証を実装しようとしています。この偉大な発見はexampleです。ただし、この例では、ポップアップウィンドウで認証を行います。私はポップアップの代わりにリダイレクトを使いたいと思う。次のようにポップアップ認証を行いMSAL Serviceためのコードは次のとおりです。私はloginRedirect代わりのloginPopupを使用して同じことを行うために、このコードを変更するにはどうすればよいloginPopupの代わりにloginRedirectを使用するには?

import { Injectable } from '@angular/core'; 
declare var bootbox: any; 
declare var Msal:any; 
@Injectable() 
export class MsalService { 

    access_token: string; 

    tenantConfig = { 
     tenant: "fabrikamb2c.onmicrosoft.com", 
     clientID: '90c0fe63-bcf2-44d5-8fb7-b8bbc0b29dc6', 
     signUpSignInPolicy: "b2c_1_susi", 
     b2cScopes: ["https://fabrikamb2c.onmicrosoft.com/demoapi/demo.read"] 
    }; 

    // Configure the authority for Azure AD B2C 

    authority = "https://login.microsoftonline.com/tfp/" + this.tenantConfig.tenant + "/" + this.tenantConfig.signUpSignInPolicy; 

    /* 
    * B2C SignIn SignUp Policy Configuration 
    */ 
    clientApplication = new Msal.UserAgentApplication(
     this.tenantConfig.clientID, this.authority, 
     function (errorDesc: any, token: any, error: any, tokenType: any) { 
      // Called after loginRedirect or acquireTokenPopup 
     } 
    ); 

    public login(): 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; 
       }, function (error: any) { 
        _this.clientApplication.acquireTokenPopup(_this.tenantConfig.b2cScopes).then(
         function (accessToken: any) { 
          _this.access_token = accessToken; 
         }, function (error: any) { 
          bootbox.alert("Error acquiring the popup:\n" + error); 
         }); 
       }) 
     }, function (error: any) { 
      bootbox.alert("Error during login:\n" + error); 
     }); 
    } 

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

    isOnline(): boolean { 
     return this.clientApplication.getUser() != null; 
    }; 
} 

答えて

2

thenにあるコードを.loginPopup(blah).then()...から受け取り、ブロックに入れると、// Called after loginRedirect or acquireTokenPopupです。

+0

あなたの答えはありがたいです。上記のコードは 'acquireTokenSilent'と' acguireTokenPopup'の両方を呼び出していますか?どちらか一方を呼び出すべきですが、両方を呼び出すべきではないようです。 – ebhh2001

+1

トークンを静かに取得できない場合は、もう一度ログインするような行動 – spottedmahn

0

ログインのためにloginPopupを呼び出す代わりに、loginRedirect関数を呼び出して、ポップアップの代わりにAzure ADとやりとりすることができます。

+0

はい、私はそんなに考えました。しかし、 '.loginPopup'(' .then(function ... ')に続く残りのコードはどうでしょうか? – ebhh2001

関連する問題