分享方式:


使用 Azure Analysis Services (AAS) 資料庫內嵌報表

適用于: 應用程式擁有資料 使用者擁有資料

本文說明如何在客戶 案例的內嵌中內 嵌使用 Azure Analysis Services (AAS) 中所儲存 資料的 Power BI 報表。 本文旨在獨立軟體發展人員 (ISV),無論資料庫是否實作 資料列層級安全性 (RLS), 都想要內嵌具有 AAS 資料的報表。

必要條件

您將需要具有與 AAS 資料庫即時連線的報表,以及具有或不含 RLS 的報表。

動態安全性 - RLS

如果您想要讓報表實作動態 RLS,請使用 函式 customeData 。 由於您無法覆寫有效的身分識別,因此建議您使用 customData 建立新的角色。 如果您將 角色取代為 customData ,您也可以使用具有 usernameuserPrincipalName 函式的角色。

請遵循下列步驟來建立新的角色,並將函 customData 式新增至角色。

  1. 在 Analysis Services 伺服器中建立角色。

    A screenshot of creating a new role in Analysis Services server.

  2. 在 [ 一般 ] 設定中,提供 [ 角色名稱 ],並將資料庫許可權設定為 [唯讀]。

    A screenshot of giving a new role a new name and setting it to read only, in the general settings in Analysis Services server.

  1. 在 [ 成員資格] 設定中,新增即將呼叫 內嵌權杖 - 產生權杖 API 的使用者。 如果您使用的服務主體不是系統管理員,請同時新增。

    A screenshot of adding users to a new role in Analysis Services server.

  2. 在 [資料列篩選] 設定中 ,使用 CUSTOMDATA() 函式設定 DAX 查詢。

    A screenshot showing how to add the function customData to the DAX query in a new role in Analysis Services server.

服務主體

如果您使用服務主體來內嵌報表,請確定服務主體是 AAS 的伺服器管理員或角色成員。 若要將 AAS 系統管理員許可權授與服務主體,請參閱 將服務主體新增至伺服器管理員角色 。 若要將服務主體新增為角色成員,請移至 成員資格 設定

使用服務主體物件識別碼 作為使用者名稱(有效身分識別)。

Analysis Service 移轉

即使有內嵌的 AAS 報表,您也可以 從 AAS 移轉至 Power BI 進階版 。 只要呼叫 內嵌權杖 - 產生權杖 API 的主體是工作區的成員或管理員,您的內嵌報表就不會在移轉期間中斷。

注意

如果服務主體不是系統管理員,而且您不想在移轉時將其設為工作區的系統管理員,請將該模型移轉至您可以為其系統管理員許可權的個別工作區。

產生內嵌權杖

使用產生 權杖 API 來產生可覆寫有效身分 識別的 內嵌權杖。

產生內嵌權杖所需的資訊取決於您連線到 Power BI 的方式( 服務主體 主要使用者 ),以及資料庫是否有 RLS。

若要產生內嵌權杖,請提供下列資訊:

  • 使用者名稱 (如果沒有 RLS 則為選擇性。RLS 的必要專案 - 使用者名稱必須與 API 呼叫者相同(在此案例中為 Master 使用者的 UPN )。 如果資料庫未使用 RLS,且未提供使用者名稱,則會使用主要使用者的認證。
  • 角色 (RLS 必要) - 只有在有效身分識別是角色的成員時,報表才會顯示資料。

範例:

針對下列三個案例之一定義使用者身分識別和角色:

  • 如果未實作 RLS:

不需要定義任何有效的身分識別。

  • 如果使用靜態 RLS:

        var rlsidentity = new EffectiveIdentity(  //If static RLS
           username: "username@contoso.com", 
           roles: new List<string>{ "MyRole" },
           datasets: new List<string>{ datasetId.ToString()}
        )
    
  • 如果使用動態 RLS:

        var rlsidentity = new EffectiveIdentity(  // If dynamic RLS
           username: "username@contoso.com",
           roles: new List<string>{ "MyRoleWithCustomData" },
           customData: "SalesPersonA"
           datasets: new List<string>{ datasetId.ToString()}
        )
    

    注意

    customData 在內嵌權杖中不能大於 1,024 個字元。

使用有效的身分識別來產生內嵌權杖:

public EmbedToken GetEmbedToken(Guid reportId, IList<Guid> datasetIds, [Optional] Guid targetWorkspaceId)
{
    PowerBIClient pbiClient = this.GetPowerBIClient();
    // Create a request for getting an embed token for the rls identity defined above
       var tokenRequest = new GenerateTokenRequestV2(
        reports: new List<GenerateTokenRequestV2Report>() { new GenerateTokenRequestV2Report(reportId) },
        datasets: datasetIds.Select(datasetId => new GenerateTokenRequestV2Dataset(datasetId.ToString())).ToList(),
        targetWorkspaces: targetWorkspaceId != Guid.Empty ? new List<GenerateTokenRequestV2TargetWorkspace>() { new GenerateTokenRequestV2TargetWorkspace(targetWorkspaceId) } : null,
        identities: new List<EffectiveIdentity> { rlsIdentity } // Only in cases of RLS
    );
    // Generate an embed token
    var embedToken = pbiClient.EmbedToken.GenerateToken(tokenRequest);
    return embedToken;
}

使用內嵌權杖將報表內嵌至您的應用程式或網站。 您的報表會根據報表中套用的 RLS 來篩選資料。