简体   繁体   中英

Power Bi: Embedding Report into MVC Site

I have created a power bi report. I want to embed this report to my MVC site. Here is my code:-

 private static readonly string ClientID = ConfigurationManager.AppSettings["ClientID"];
 private static readonly string ClientSecret = ConfigurationManager.AppSettings["ClientSecret"];
 private static readonly string RedirectUrl = ConfigurationManager.AppSettings["RedirectUrl"];
 private static readonly string AADAuthorityUri = ConfigurationManager.AppSettings["AADAuthorityUri"];
 private static readonly string PowerBiAPI = ConfigurationManager.AppSettings["PowerBiAPI"];
 private static readonly string PowerBiDataset = ConfigurationManager.AppSettings["PowerBiDataset"];
 private static readonly string baseUri = PowerBiDataset;
 private static string accessToken = string.Empty;
 public class PBIReports
 {
     public PBIReport[] value { get; set; }
 }
 public class PBIReport
 {
     public string id { get; set; }
     public string name { get; set; }
     public string webUrl { get; set; }
     public string embedUrl { get; set; }
 }

 public ReportController()
 {
     try
     {
         if (Request.Params.Get("code") != null)
         {

             Session["AccessToken"] = GetAccessToken(
                 HttpContext.Request["code"],
                 ClientID,
                 ClientSecret,
                 RedirectUrl);
         }
         if (Session["AccessToken"] != null)
         {
             accessToken = Session["AccessToken"].ToString();
             GetReport(0);
         }
     }
     catch(Exception ex)
     {

     }
 }

 public string GetAccessToken(string authorizationCode, string clientID, string clientSecret, string redirectUri)
 {      
     TokenCache TC = new TokenCache();
     string authority = AADAuthorityUri;
     AuthenticationContext AC = new AuthenticationContext(authority, TC);
     ClientCredential cc = new ClientCredential(clientID, clientSecret);
     return AC.AcquireTokenByAuthorizationCodeAsync(
         authorizationCode,
         new Uri(redirectUri), cc).Result.AccessToken;
 }
 protected void GetReport(int index)
 {
     System.Net.WebRequest request = System.Net.WebRequest.Create(
         String.Format("{0}/Reports",
         baseUri)) as System.Net.HttpWebRequest;

     request.Method = "GET";
     request.ContentLength = 0;
     request.Headers.Add("Authorization", String.Format("Bearer {0}", accessToken));
     using (var response = request.GetResponse() as System.Net.HttpWebResponse)
     {
         using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
         {
             PBIReports Reports = JsonConvert.DeserializeObject<PBIReports>(reader.ReadToEnd());
             if (Reports.value.Length > 0)
             {
                 var report = Reports.value[index];

                 ViewBag["ReportId"] = report.id;
                 ViewBag["EmbedUrl"] = report.embedUrl;
                 ViewBag["ReportName"] = report.name;
                 ViewBag["WebUrl"] = report.webUrl;
             }
         }
     }
 }

 public void GetAuthorizationCode()
 {
     var @params = new NameValueCollection
     {
         {"response_type", "code"},
         {"client_id", ClientID},
         {"resource", PowerBiAPI},
         { "redirect_uri", RedirectUrl}
     };

     var queryString = HttpUtility.ParseQueryString(string.Empty);
     queryString.Add(@params);
     Response.Redirect(String.Format(AADAuthorityUri + "?{0}", queryString));
 }

 public ActionResult Index()
 {
     GetAuthorizationCode();
     return View();
 }

On Redirecting to "Report Page", it goes for power bi login page and after I sign in it redirects back to this page (as homepage and redirect url are same). But Request.Params.Get("code") != null is null. HttpContext.Request is also null. Why? Is there anything wrong in the code?

In the constructor of a controller you don't have the this.HttpContext object to your disposal yet. You might have HttpContext.Current , but I am not sure of that.

What you should do is move that code to an action (for example your GetReport action), where you can make your checks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM