Forum Discussion

Mexicoder's avatar
Mexicoder
Frequent Visitor
8 years ago
Solved

Api report custom header add role

  public async Task<ActionResult> GetReportEmbedded() { // Create a user password credentials. var accessToken = await GetTokenCredentials(); var token...
  • Mexicoder's avatar
    8 years ago

    so i still dont know how to use custom header but i figured ouut how to add roles using the GenerateTokenRequest object and the GenerateTokenInGroupWithHttpMessagesAsync call.

     

     

        private async Task<string> GetTokenCredentials()
            {
                var credential = new UserPasswordCredential(powerBi.Value.Username, powerBi.Value.Password);
    
                // Authenticate using created credentials
                var authenticationContext = new AuthenticationContext(powerBi.Value.AuthorityUrl);
                var authenticationResult =
                    await authenticationContext.AcquireTokenAsync(powerBi.Value.ResourceUrl, powerBi.Value.ClientId,
                        credential);
    
                if (authenticationResult == null)
                {
                    Console.WriteLine("Authentication Failed");
                    return string.Empty;
                }
                return authenticationResult.AccessToken;
            }
    
    
    
            public async Task<ActionResult> GetReportEmbedded()
            {
                // Create a user password credentials.
                var accessToken = await GetTokenCredentials();
    
                var tokenCredentials = new TokenCredentials(accessToken, "Bearer");
    
                // Generate Embed Token.
    
                using (var client = new PowerBIClient(new Uri(powerBi.Value.ApiUrl), tokenCredentials))
                using (Task<Group> tableTalkGroup = client.Groups.GetGroupsWithHttpMessagesAsync()
                    .ContinueWith(task => task.Result.Body.Value.First(group => @group.Name == "Tuku TEST")))
                using (Task<Report> reportTask = client.Reports.GetReportsInGroupWithHttpMessagesAsync((await tableTalkGroup).Id).ContinueWith(task => task.Result.Body.Value.First(report => report.Name == "DirectQueryPC")))
    
                {
                    var generateTokenRequestParameters = new GenerateTokenRequest(
                        accessLevel: "View",
                        datasetId: null,
                        identities: new List<EffectiveIdentity>
                        {
                            new EffectiveIdentity(
                                username: powerBi.Value.Username,
                                roles: new List<string> { "filterKel" },
                                datasets: new List<string> { (await reportTask).DatasetId })
                        });
    
                    var tokenResponse = await client.Reports.GenerateTokenInGroupWithHttpMessagesAsync(
                        groupId: (await tableTalkGroup).Id,
                        reportKey: (await reportTask).Id,
                        requestParameters: generateTokenRequestParameters);
                    // Generate Embed Configuration.
                    var embedConfig = new EmbedConfig()
                    {
                        EmbedToken = tokenResponse.Body,
                        EmbedUrl = (await reportTask).EmbedUrl,
                        Id = (await reportTask).Id
                    };
    
                    return View(embedConfig);
                }
            }