簡體   English   中英

如何為adal-angular6配置生成動態數據

[英]how to generate dynamic data for adal-angular6 configuration

我目前正在連接AAD和azure graph api,獲取我所需要的一切以及它的優點。 我的問題是我的'adalConfig'屬性是用這樣的硬編碼編寫的:

在app.module>導入:

         c.MsAdalAngular6Module.forRoot({
         tenant: '080werg-1e3r-5dnb-8c3b-e37ttrr8ee8',
         clientId: '080werg-080werg-080werg-080werg-080werg',
         redirectUri: window.location.origin,
         endpoints: {
            "https://cloudcrp-client.azurewebsites.net": "080werg- 80werg- 
             080werg- 080werg- 080werg",
         },
         navigateToLoginRequestUrl: false,
    }),

這對我來說非常糟糕,因為我們有幾個客戶,每個客戶都需要不同的信息。 我想要做的是從服務器獲取具有正確詳細信息的信息。 所以基本上以異步的方式使用.forRoot。

我發現這篇文章: https//devblogs.microsoft.com/premier-developer/angular-how-to-microsoft-adal-for-angular-6-with-configurable-settings/我認為它與之相關我想實現,但我無法弄明白。

TIA

所以我終於做到了,畢竟這並不困難。 我走過了所有“adal”配置的步驟。 請參閱以下文章,它有很多幫助: https//devblogs.microsoft.com/premier-developer/angular-how-to-microsoft-adal-for-angular-6-with-configurable-settings/

app.module.ts:

export let adalConfig
export function getConfig(){
    return adalConfig
}

將使用上述函數(在providers部分中)為providers數組提供adalConfig變量。

export function loadConfigurations(connectionService: ConnectionService) {
    return () => connectionService.getConfigs().then(((adalConficObj: azureActiveDirectoryModel)=> {
        adalConfig = {
            tenant: adalConficObj.Tenant,
            clientId: adalConficObj.ClientID,
            redirectUri: window.location.origin,
            endpoints: {
                [adalConficObj.EndPoint]: adalConficObj.ObjectID,
            },
            navigateToLoginRequestUrl: false,
        }                
    }));
}

在上面的函數中,我返回一個函數,它解析了一個Promise(我將在APP_INITIALIZER中使用這個函數,誰需要一個Promise - 否則你會得到一個錯誤)Promise來自My connectionService,在我的app中負責用於連接到服務器。 getConfigs()很簡單,如下所示:

    getConfigs(): Promise<Object> {
        return this.HttpClient.get(this.getURL('GetAzureActiveDirectoryconfiguration')).toPromise()

    }

http / httpClient使用observables和subscription,但由於我想要一個Promise,我使用.toPromise函數。

如果您已閱讀該文章,您會看到我沒有使用forRoot()啟動MsAdalAngular6Module,因為我想從服務器獲取所有adalConfig信息。

imports: [

    c.MsAdalAngular6Module,

],

根據我的理解,MsAdalAngular6Module將使用提供者數組中提供的變量來構造MsAdalAngular6Service(再次使用forRoot()中的硬編碼進行修復)

app.modules中的providers數組:


        {
            provide: APP_INITIALIZER,
            useFactory: loadConfigurations,
            deps: [ConnectionService], // dependancy
            multi: true
        },
        {
            provide: 'adalConfig',
            useFactory: getConfig,
            deps: []
        },

            MsAdalAngular6Service

APP_INITIALIZER基本上停止啟動app,直到useFactory中提供的功能完成。 在下一節中,我們使用“給”提供者數組我們的adalConfig對象,在它具有相關數據之后。 我們提供變量本身(提供:'adalConfig')所以這個函數可以返回它(useFactory:getConfig,)因為getConfig()不使用傳遞給它的任何屬性,deps:是多余的。

我真的希望這有幫助,如果我出錯了,請糾正我。

有關詳細信息,請參閱: https ://angular.io/guide/dependency-injection-providers Angular:如何正確實現APP_INITIALIZER

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM