Embed power bi on an on premises asp net web app with windows authentication

Introduction

ASP.NET is a popular programming language used for developing web applications. One common requirement in web development is embedding BI in an on-premises ASP.NET web app with Windows authentication. In this article, we will explore how to achieve this using ASP.NET and provide examples to illustrate the process.

Step 1: Set up Power BI Embedded

we can embed Power BI reports, we need to set up Power BI Embedded in Azure. Follow these steps:

  1. Create a Power BI Embedded capacity in Azure portal.
  2. an access key for the capacity.
  3. Register your application in Azure Active Directory (AAD) and grant it access to Power BI service.

Once you have these steps, you are ready to proceed with embedding Power BI reports in your ASP.NET web app.

Step 2: Install Power BI .NET SDK

To interact with Power BI Embedded, we need to install the Power BI .NET SDK. You can do this by adding the following package to your ASP.NET project:

Install-Package Microsoft.PowerBI.Api

This package provides the necessary classes and methods to work with Power BI Embedded in your ASP.NET application.

Step 3: Authenticate with Power BI

Since we are using Windows authentication in our ASP.NET web app, we need to authenticate with Power BI using the access key generated earlier. Here's an of how to authenticate:

using Microsoft.PowerBI.Api;
using Microsoft.PowerBI.Api.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

public class PowerBIService
{
    private static readonly string accessKey = "YOUR_ACCESS_KEY";
    private static readonly string authorityUrl = "https://login.windows.net/YOUR_TENANT_ID";
    private static readonly string resourceUrl = "https://analysis.windows.net/powerbi/api";

    public static PowerBIClient Authenticate()
    {
        var authenticationContext = new AuthenticationContext(authorityUrl);
        var credential = new ClientCredential(accessKey, accessKey);
        var authenticationResult = authenticationContext.AcquireTokenAsync(resourceUrl, credential).Result;

        var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
        var client = new PowerBIClient(new Uri(resourceUrl), tokenCredentials);

        return client;
    }
}

In this example, replace “YOUR_ACCESS_KEY” with the access key generated in Azure portal and “YOUR_TENANT_ID” with your Azure AD tenant ID.

Step 4: Embed Power BI Report

Now that we have authenticated with Power BI, we can embed a Power BI report in our ASP.NET web app. Here's an example:

using Microsoft.PowerBI.Api;
using Microsoft.PowerBI.Api.Models;

public class PowerBIEmbedService
{
    public static EmbedConfig EmbedReport( reportId, string groupId, string embedToken)
    {
        var client = PowerBIService.Authenticate();

        var report = client.Reports.GetReportInGroup(groupId, reportId);
        var embedUrl = report.EmbedUrl;

        var config = new EmbedConfig
        {
            EmbedUrl = embedUrl,
            EmbedToken = embedToken
        };

        return config;
    }
}

In this example, replace “reportId” and “groupId” with the respective IDs of the Power BI report you want to embed. The “embedToken” is a token obtained from Power BI service that grants access to the report.

Step 5: Display Embedded Power BI Report

Finally, we can display the embedded Power BI report in our ASP.NET web app. Here's an example:

In this example, replace “EMBED_URL”, “ACCESS_TOKEN”, and “REPORT_ID” with the respective values obtained from the steps. The “reportContainer” is a div element the Power BI report will be rendered.

Conclusion

Embedding Power BI reports in an on-premises ASP.NET web app with Windows authentication is achievable using ASP.NET and the Power BI .NET SDK. By following the steps outlined in this article and using the provided examples, you can successfully integrate Power BI reports into your ASP.NET web app.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents