Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Identity.Client" Version="4.21.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.59.0" />
<PackageReference Include="Microsoft.PowerBI.Api" Version="3.21.0" />
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.76" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace AppOwnsData.Controllers
using Microsoft.Extensions.Options;
using System;
using System.Text.Json;
using Microsoft.Rest;
using System.Linq;
using System.Threading.Tasks;

public class EmbedInfoController : Controller
{
Expand All @@ -30,7 +33,7 @@ public EmbedInfoController(PbiEmbedService pbiEmbedService, IOptions<AzureAd> az
/// </summary>
/// <returns>JSON containing parameters for embedding</returns>
[HttpGet]
public string GetEmbedInfo()
public async Task<string> GetEmbedInfoAsync()
{
try
{
Expand All @@ -42,9 +45,15 @@ public string GetEmbedInfo()
return configValidationResult;
}

EmbedParams embedParams = pbiEmbedService.GetEmbedParams(new Guid(powerBI.Value.WorkspaceId), new Guid(powerBI.Value.ReportId));
EmbedParams embedParams = await pbiEmbedService.GetEmbedParams(new Guid(powerBI.Value.WorkspaceId), new Guid(powerBI.Value.ReportId));
return JsonSerializer.Serialize<EmbedParams>(embedParams);
}
catch (HttpOperationException exc)
{
HttpContext.Response.StatusCode = (int)exc.Response.StatusCode;
var message = string.Format("Status: {0} ({1})\r\nResponse: {2}\r\nRequestId: {3}", exc.Response.StatusCode, (int)exc.Response.StatusCode, exc.Response.Content, exc.Response.Headers["RequestId"].FirstOrDefault());
return message;
}
catch (Exception ex)
{
HttpContext.Response.StatusCode = 500;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace AppOwnsData.Services
using System;
using System.Linq;
using System.Security;
using System.Threading.Tasks;

public class AadService
{
Expand All @@ -25,18 +26,19 @@ public AadService(IOptions<AzureAd> azureAd)
/// Generates and returns Access token
/// </summary>
/// <returns>AAD token</returns>
public string GetAccessToken()
public async Task<string> GetAccessToken()
{
AuthenticationResult authenticationResult = null;
if (azureAd.Value.AuthenticationMode.Equals("masteruser", StringComparison.InvariantCultureIgnoreCase))
{
// Create a public client to authorize the app with the AAD app
IPublicClientApplication clientApp = PublicClientApplicationBuilder.Create(azureAd.Value.ClientId).WithAuthority(azureAd.Value.AuthorityUrl).Build();
var userAccounts = clientApp.GetAccountsAsync().Result;
var userAccounts = await clientApp.GetAccountsAsync();

try
{
// Retrieve Access token from cache if available
authenticationResult = clientApp.AcquireTokenSilent(azureAd.Value.ScopeBase, userAccounts.FirstOrDefault()).ExecuteAsync().Result;
authenticationResult = await clientApp.AcquireTokenSilent(azureAd.Value.ScopeBase, userAccounts.FirstOrDefault()).ExecuteAsync();
}
catch (MsalUiRequiredException)
{
Expand All @@ -45,7 +47,7 @@ public string GetAccessToken()
{
password.AppendChar(key);
}
authenticationResult = clientApp.AcquireTokenByUsernamePassword(azureAd.Value.ScopeBase, azureAd.Value.PbiUsername, password).ExecuteAsync().Result;
authenticationResult = await clientApp.AcquireTokenByUsernamePassword(azureAd.Value.ScopeBase, azureAd.Value.PbiUsername, password).ExecuteAsync();
}
}

Expand All @@ -62,7 +64,7 @@ public string GetAccessToken()
.WithAuthority(tenantSpecificUrl)
.Build();
// Make a client call if Access token is not available in cache
authenticationResult = clientApp.AcquireTokenForClient(azureAd.Value.ScopeBase).ExecuteAsync().Result;
authenticationResult = await clientApp.AcquireTokenForClient(azureAd.Value.ScopeBase).ExecuteAsync();
}

return authenticationResult.AccessToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace AppOwnsData.Services
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

public class PbiEmbedService
{
Expand All @@ -28,19 +29,20 @@ public PbiEmbedService(AadService aadService)
/// Get Power BI client
/// </summary>
/// <returns>Power BI client object</returns>
public PowerBIClient GetPowerBIClient()
public async Task<PowerBIClient> GetPowerBIClient()
{
var tokenCredentials = new TokenCredentials(aadService.GetAccessToken(), "Bearer");
var accessToken = await aadService.GetAccessToken();
var tokenCredentials = new TokenCredentials(accessToken, "Bearer");
return new PowerBIClient(new Uri(powerBiApiUrl ), tokenCredentials);
}

/// <summary>
/// Get embed params for a report
/// </summary>
/// <returns>Wrapper object containing Embed token, Embed URL, Report Id, and Report name for single report</returns>
public EmbedParams GetEmbedParams(Guid workspaceId, Guid reportId, [Optional] Guid additionalDatasetId)
public async Task<EmbedParams> GetEmbedParams(Guid workspaceId, Guid reportId, [Optional] Guid additionalDatasetId)
{
PowerBIClient pbiClient = this.GetPowerBIClient();
PowerBIClient pbiClient = await this.GetPowerBIClient();

// Get report info
var pbiReport = pbiClient.Reports.GetReportInGroup(workspaceId, reportId);
Expand All @@ -55,7 +57,7 @@ public EmbedParams GetEmbedParams(Guid workspaceId, Guid reportId, [Optional] Gu
if (isRDLReport)
{
// Get Embed token for RDL Report
embedToken = GetEmbedTokenForRDLReport(workspaceId, reportId);
embedToken = await GetEmbedTokenForRDLReport(workspaceId, reportId);
}
else
{
Expand All @@ -72,7 +74,7 @@ public EmbedParams GetEmbedParams(Guid workspaceId, Guid reportId, [Optional] Gu
}

// Get Embed token multiple resources
embedToken = GetEmbedToken(reportId, datasetIds, workspaceId);
embedToken = await GetEmbedToken(reportId, datasetIds, workspaceId);
}

// Add report data for embedding
Expand All @@ -99,11 +101,11 @@ public EmbedParams GetEmbedParams(Guid workspaceId, Guid reportId, [Optional] Gu
/// </summary>
/// <returns>Wrapper object containing Embed token, Embed URL, Report Id, and Report name for multiple reports</returns>
/// <remarks>This function is not supported for RDL Report</remakrs>
public EmbedParams GetEmbedParams(Guid workspaceId, IList<Guid> reportIds, [Optional] IList<Guid> additionalDatasetIds)
public async Task<EmbedParams> GetEmbedParams(Guid workspaceId, IList<Guid> reportIds, [Optional] IList<Guid> additionalDatasetIds)
{
// Note: This method is an example and is not consumed in this sample app

PowerBIClient pbiClient = this.GetPowerBIClient();
PowerBIClient pbiClient = await this.GetPowerBIClient();

// Create mapping for reports and Embed URLs
var embedReports = new List<EmbedReport>();
Expand All @@ -130,7 +132,7 @@ public EmbedParams GetEmbedParams(Guid workspaceId, IList<Guid> reportIds, [Opti
}

// Get Embed token multiple resources
var embedToken = GetEmbedToken(reportIds, datasetIds, workspaceId);
var embedToken = await GetEmbedToken(reportIds, datasetIds, workspaceId);

// Capture embed params
var embedParams = new EmbedParams
Expand All @@ -148,9 +150,9 @@ public EmbedParams GetEmbedParams(Guid workspaceId, IList<Guid> reportIds, [Opti
/// </summary>
/// <returns>Embed token</returns>
/// <remarks>This function is not supported for RDL Report</remakrs>
public EmbedToken GetEmbedToken(Guid reportId, IList<Guid> datasetIds, [Optional] Guid targetWorkspaceId)
public async Task<EmbedToken> GetEmbedToken(Guid reportId, IList<Guid> datasetIds, [Optional] Guid targetWorkspaceId)
{
PowerBIClient pbiClient = this.GetPowerBIClient();
PowerBIClient pbiClient = await this.GetPowerBIClient();

// Create a request for getting Embed token
// This method works only with new Power BI V2 workspace experience
Expand All @@ -174,11 +176,11 @@ public EmbedToken GetEmbedToken(Guid reportId, IList<Guid> datasetIds, [Optional
/// </summary>
/// <returns>Embed token</returns>
/// <remarks>This function is not supported for RDL Report</remakrs>
public EmbedToken GetEmbedToken(IList<Guid> reportIds, IList<Guid> datasetIds, [Optional] Guid targetWorkspaceId)
public async Task<EmbedToken> GetEmbedToken(IList<Guid> reportIds, IList<Guid> datasetIds, [Optional] Guid targetWorkspaceId)
{
// Note: This method is an example and is not consumed in this sample app

PowerBIClient pbiClient = this.GetPowerBIClient();
PowerBIClient pbiClient = await this.GetPowerBIClient();

// Convert report Ids to required types
var reports = reportIds.Select(reportId => new GenerateTokenRequestV2Report(reportId)).ToList();
Expand Down Expand Up @@ -208,11 +210,11 @@ public EmbedToken GetEmbedToken(IList<Guid> reportIds, IList<Guid> datasetIds, [
/// </summary>
/// <returns>Embed token</returns>
/// <remarks>This function is not supported for RDL Report</remakrs>
public EmbedToken GetEmbedToken(IList<Guid> reportIds, IList<Guid> datasetIds, [Optional] IList<Guid> targetWorkspaceIds)
public async Task<EmbedToken> GetEmbedToken(IList<Guid> reportIds, IList<Guid> datasetIds, [Optional] IList<Guid> targetWorkspaceIds)
{
// Note: This method is an example and is not consumed in this sample app

PowerBIClient pbiClient = this.GetPowerBIClient();
PowerBIClient pbiClient = await this.GetPowerBIClient();

// Convert report Ids to required types
var reports = reportIds.Select(reportId => new GenerateTokenRequestV2Report(reportId)).ToList();
Expand Down Expand Up @@ -248,9 +250,9 @@ public EmbedToken GetEmbedToken(IList<Guid> reportIds, IList<Guid> datasetIds, [
/// Get Embed token for RDL Report
/// </summary>
/// <returns>Embed token</returns>
public EmbedToken GetEmbedTokenForRDLReport(Guid targetWorkspaceId, Guid reportId, string accessLevel = "view")
public async Task<EmbedToken> GetEmbedTokenForRDLReport(Guid targetWorkspaceId, Guid reportId, string accessLevel = "view")
{
PowerBIClient pbiClient = this.GetPowerBIClient();
PowerBIClient pbiClient = await this.GetPowerBIClient();

// Generate token request for RDL Report
var generateTokenRequestParameters = new GenerateTokenRequest(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="1.1.0" />
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="2.15.3" />
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.0.96" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.1.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.17.1" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.1.0" />
<PackageReference Include="Microsoft.PowerBI.Api" Version="3.16.0" />
</ItemGroup>
Expand Down
Loading