2017-07-04 28 views
0

パワーバイレポートを作成しました。このレポートをMVCサイトに埋め込みたいのですが。 (ホームページのようにURLをリダイレクト同じです)、それは電源バイログインページのために行くと、私はそれに署名した後、このページに戻ってリダイレクトする「レポートページ」にリダイレクトするにはパワーBi:MVCサイトにレポートを埋め込む

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(); 
} 

- :ここに私のコードです。ただし、Request.Params.Get("code") != nullはnullです。 HttpContext.Requestもnullです。どうして?コードに間違いはありますか?

+0

@Patrick Hofman確認してください。https://stackoverflow.com/questions/44906078/embedding-power-bi-report-promise-is-not-defined-powerbi-js – Sonali

答えて

1

コントローラーのコンストラクターでは、処理するオブジェクトがまだありません。this.HttpContextHttpContext.Currentがあるかもしれませんが、私はそれについては分かりません。

あなたがする必要があるのは、あなたのチェックを行うことができるアクション(たとえばGetReportアクション)にそのコードを移動することです。

+0

リダイレクトをどのように管理しますか?コードを取得する。ループでのみ混乱します – Sonali

+0

どのようなリダイレクトを意味しますか? –

+0

ユーザーがこのページに移動すると、アクセストークンが利用できない場合、サインインした後にサインインすると、クエリ文字列のcode = accesstokenで同じURLにリダイレクトされます。これを使用してアクセストークンを呼び出し、レポートを取得します。 – Sonali

関連する問題