title: Power BI report is not loading in MVC application
body: <p>Few days back i learnt making reports in power BI and it was great experience learning power BI. As i am creating a dashboard for my MVC based web application, i wanted to make look and fill of my dashboard attractive. I am thinking of embedding power BI report with that for that i have used following code inside view :-</p>
  
  <pre><code>&lt;body&gt;
      &lt;script type="text/javascript" src="~/Scripts/PowerBI/powerbi.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
      window.onload = function () {
          var iframe = document.getElementById("iFrameEmbedReport");
          iframe.src = "https://app.powerbi.com/reportEmbed?reportId=" + embedReportId;
          iframe.onload = postActionLoadReport;
      }
  
      function postActionLoadReport() {
          var m = {
              action: "loadReport",
              accessToken: accessToken
          };
          message = JSON.stringify(m);
  
          iframe = document.getElementById("iFrameEmbedReport");
          iframe.contentWindow.postMessage(message, "*");;
        }
      &lt;/script&gt;
      &lt;style&gt;
          #iFrameEmbedReport {
              width: 95%;
              height: 95%;
          }
      &lt;/style&gt;
      &lt;iframe ID="iFrameEmbedReport"&gt;&lt;/iframe&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>And code for controller is  given below :-</p>
  
  <pre><code>public class DashBoardController : Controller
      {
          string baseUri = WebConfigurationManager.AppSettings["PowerBiDataset"];
          string AccessToken = string.Empty;
          // GET: DashBoard
          public ActionResult DashBoard()
          {
  
              if (Request.Params.Get("code") != null)
              {
                  Session["AccessToken"] = GetAccessToken(
                      Request.Params.GetValues("code")[0],
                      WebConfigurationManager.AppSettings["ClientID"],
                      WebConfigurationManager.AppSettings["ClientSecret"],
                      WebConfigurationManager.AppSettings["RedirectUrl"]);
  
  
                  Response.Redirect("~/DashBoard/DashBoard");
              }
  
              if (Session["AccessToken"] != null)
              {
  
                  AccessToken = Session["AccessToken"].ToString();
                  GetReport(0);
              }
              return View();
          }
  
  
          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&lt;PBIReports&gt;(reader.ReadToEnd());
  
                      if (Reports.value.Length &gt; 0)
                      {
                          var report = Reports.value[index];
                          ViewData["AccessToken"] = Session["AccessToken"].ToString();
                          ViewData["EmbedURL"] = report.embedUrl;
                          ViewData["ReportID"] = report.id;
                      }
                  }
              }
          }
  
          public void GetAuthorizationCode()
          {
  
              var @params = new NameValueCollection
              {
  
                  {"response_type", "code"},
                  {"client_id", WebConfigurationManager.AppSettings["ClientID"]},
                  {"resource", WebConfigurationManager.AppSettings["PowerBiAPI"]},
                  { "redirect_uri", WebConfigurationManager.AppSettings["RedirectUrl"]}
              };
  
              var queryString = HttpUtility.ParseQueryString(string.Empty);
              queryString.Add(@params);
  
              Response.Redirect(String.Format(WebConfigurationManager.AppSettings["AADAuthorityUri"] + "?{0}", queryString));
  
  
          }
  
          public string GetAccessToken(string authorizationCode, string clientID, string clientSecret, string redirectUri)
          {
              TokenCache TC = new TokenCache();
  
              string authority = WebConfigurationManager.AppSettings["AADAuthorityUri"];
              AuthenticationContext AC = new AuthenticationContext(authority, TC);
              ClientCredential cc = new ClientCredential(clientID, clientSecret);
  
              return AC.AcquireTokenByAuthorizationCode(
                  authorizationCode,
                  new Uri(redirectUri), cc).AccessToken;
          }
      }
      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; }
      }
  </code></pre>
  
  <p>As i think, I am doing everything right but i don't know why it's not able to display report. Please suggest me if i did any mistake in the above given code.</p>