由于缺少有效身份中的角色,因此无法生成用于访问数据集的嵌入令牌

时间:2019-05-09 00:19:48

标签: powerbi powerbi-embedded powerbi-desktop

我嵌入了powerbi报告,该报告在更改数据库之前一直运行良好。

我观察到数据集。IsEffectiveIdentityRequired(在下面的代码中)之前为false,现在为true,我收到一个错误-{“ error”:{“ code”:“ InvalidRequest”,“ message”:“正在创建嵌入令牌以访问数据集02c90e15-35dd-4036-a525-4f5d158bfade要求角色包含在提供的有效身份中。“}}

我正在使用标准的嵌入服务代码。

//创建一个Power BI Client对象。它将用于调用Power BI API。

            using (var client = new PowerBIClient(new Uri(ApiUrl), m_tokenCredentials))
            {
                // Get a list of reports.
                var reports = await client.Reports.GetReportsInGroupAsync(WorkspaceId);


                Report report = reports.Value.FirstOrDefault(r => r.Id.Equals(ReportId, StringComparison.InvariantCultureIgnoreCase));

                var datasets = await client.Datasets.GetDatasetByIdInGroupAsync(WorkspaceId, report.DatasetId);
                m_embedConfig.IsEffectiveIdentityRequired = datasets.IsEffectiveIdentityRequired;
                m_embedConfig.IsEffectiveIdentityRolesRequired = datasets.IsEffectiveIdentityRolesRequired;
                GenerateTokenRequest generateTokenRequestParameters;
                // This is how you create embed token with effective identities
                // HERE username IS NULL
                if (!string.IsNullOrWhiteSpace(username))
                {
                    var rls = new EffectiveIdentity(username, new List<string> { report.DatasetId });
                    if (!string.IsNullOrWhiteSpace(roles))
                    {
                        var rolesList = new List<string>();
                        rolesList.AddRange(roles.Split(','));
                        rls.Roles = rolesList;
                    }
                    // Generate Embed Token with effective identities.
                    generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view", identities: new List<EffectiveIdentity> { rls });
                }
                else
                {
                    // Generate Embed Token for reports without effective identities.
                    generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
                }

                var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(WorkspaceId, report.Id, generateTokenRequestParameters);

}

首先,我完全理解由于未传递任何身份信息而发生此错误。那么,是否有任何选项可以禁用IsEffectiveIdentityRequired?

第二,如何在powerbi中设置用户和角色? -我不是PowerBI专家-

1 个答案:

答案 0 :(得分:0)

IsEffectiveIdentityRequired 是只读属性,因此您无法控制它,也没有选择禁用它。

取决于连接到有效身份的数据源,可能需要也可能不需要。

如果IsEffectiveIdentityRequired为true,则在调用GenerateTokenRequest生成嵌入令牌时需要传递EffectiveIdentity。如果数据源需要有效的身份,而您没有传递有效的身份,则在调用GenerateTokenRequest时会收到错误消息。如果传递不完整的EffectiveIdentity(例如,在调用GenerateTokenRequest时缺少角色),也会收到一条错误消息。

这里是一个示例,说明如何使用IsEffectiveIdentityRequired属性生成带有或不带有有效身份的嵌入令牌,具体取决于数据源是否需要它。

                List<EffectiveIdentity> eil = new List<EffectiveIdentity>();
                EffectiveIdentity ef = new EffectiveIdentity();

                // UserName
                ef.Username = FullADUsername;

                // Roles
                List<string> Roles = new List<string>();

                ef.Roles = Roles;

                // Datasets
                List<string> _Datasets = new List<string>();
                _Datasets.Add(report.DatasetId);
                ef.Datasets = _Datasets;

                eil.Add(ef);

                // Look up the data set of the report and look if we need to pass an Effective Identify               
                Dataset d = client.Datasets.GetDatasetByIdInGroup(WorkspaceId, report.DatasetId);
                if (d.IsEffectiveIdentityRequired == true)
                {
                    GenerateTokenRequest gtr = new GenerateTokenRequest("View", null, false, eil);
                    newEmbedToken = client.Reports.GenerateTokenInGroup(WorkspaceId, ReportId, gtr);
                }
                else
                {
                    GenerateTokenRequest gtr = new GenerateTokenRequest();
                    newEmbedToken = client.Reports.GenerateTokenInGroup(WorkspaceId, ReportId, gtr);
                }