Esempio n. 1
0
        protected void renderReport()
        {
            Report report;
            var    request = this.CreateRequest();

            request.Method        = "GET";
            request.ContentLength = 0;
            request.Headers.Add("Authorization", String.Format("AppKey {0}", accessKey));

            using (var response = request.GetResponse() as HttpWebResponse)
            {
                using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    var serializer = new JavaScriptSerializer();
                    var reports    = serializer.Deserialize <ODataResponseListReport>(reader.ReadToEnd());
                    report = reports.Value.FirstOrDefault(r => r.Id == reportID);

                    string myUserID   = ddlUser.SelectedValue;
                    var    embedToken = (myUserID != "" ? PowerBIToken.CreateReportEmbedToken(workspaceCollection, workspaceId, report.Id, myUserID, new string[] { "Participante" }) : PowerBIToken.CreateReportEmbedToken(workspaceCollection, workspaceId, report.Id, "0", null));
                    string myTok      = embedToken.Generate(accessKey);

                    accessTokenText.Value = myTok;                                                                    //input on the report page.
                    embedUrlText.Value    = "https://embedded.powerbi.com/appTokenReportEmbed?reportId=" + report.Id; //input on the report page.
                }
            }
        }
Esempio n. 2
0
        public async Task <ActionResult> Report(string reportId, string userName, string rolesCSV)
        {
            using (var client = this.CreatePowerBIClient())
            {
                var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);

                var report = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);

                PowerBIToken embedToken = null;

                var userNameResolve = string.IsNullOrWhiteSpace(userName) ? null : userName.Trim();
                var roles           = string.IsNullOrWhiteSpace(rolesCSV) ? null : rolesCSV.Split(new char[] { ',' }).Select(r => r.Trim());
                embedToken = PowerBIToken.CreateReportEmbedToken(
                    workspaceCollectionName: this.workspaceCollection,
                    workspaceId: this.workspaceId,
                    reportId: report.Id,
                    username: userNameResolve,
                    roles: roles
                    );



                var viewModel = new ReportViewModel
                {
                    Report      = report,
                    AccessToken = embedToken.Generate(this.accessKey)
                };

                return(View(viewModel));
            }
        }
Esempio n. 3
0
        public static ReportsController.FetchReportResult FetchReport(string reportId)
        {
            // Create a dev token for fetch
            var devToken = PowerBIToken.CreateDevToken(WorkspaceCollection, WorkspaceId);

            using (var client = CreatePowerBiClient(devToken))
            {
                var reports = ReportsExtensions.GetReports(client.Reports, WorkspaceCollection, WorkspaceId);
                var report  = reports.Value.FirstOrDefault(r => r.Id == reportId);

                if (report != null)
                {
                    var embedToken = PowerBIToken.CreateReportEmbedToken(WorkspaceCollection, WorkspaceId, report.Id);

                    var result = new ReportsController.FetchReportResult
                    {
                        Report      = report,
                        AccessToken = embedToken.Generate(ConfigHelper.PowerbiSigningKey)
                    };

                    return(result);
                }

                return(new ReportsController.FetchReportResult()
                {
                    AccessToken = string.Empty,
                    Report = null
                });
            }
        }
Esempio n. 4
0
        public async Task <ActionResult> Report(string reportId)
        {
            try
            {
                var devToken = PowerBIToken.CreateDevToken(this.workspaceCollection, this.workspaceId);
                using (var client = this.CreatePowerBIClient(devToken))
                {
                    var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);

                    var report     = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);
                    var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id);

                    var viewModel = new ReportViewModel
                    {
                        Report      = report,
                        AccessToken = embedToken.Generate(this.accessKey)
                    };

                    return(View(viewModel));
                }
            }
            catch (Exception ex)
            {
                StorageClient.LogError(ex);
            }
            return(null);
        }
        public void CreateReportEmbedTokenMissingReportAndDatasetIdsFails()
        {
            var workspaceId = Guid.NewGuid().ToString();
            var reportId    = Guid.NewGuid().ToString();

            PowerBIToken.CreateReportEmbedToken("Contoso", workspaceId, null);
        }
        public void CreateReportEmbedTokenWithInvalidExpirationFails()
        {
            var workspaceId = Guid.NewGuid().ToString();
            var reportId    = Guid.NewGuid().ToString();

            PowerBIToken.CreateReportEmbedToken("Contoso", workspaceId, reportId, DateTime.MinValue);
        }
        public static ReportsController.FetchReportResult FetchReport(string reportId)
        {
            using (var client = CreatePowerBiClient())
            {
                var reportsResponse = client.Reports.GetReports(WorkspaceCollection, WorkspaceId);
                var report          = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);

                if (report != null)
                {
                    var embedToken = PowerBIToken.CreateReportEmbedToken(WorkspaceCollection, WorkspaceId, report.Id);

                    return(new ReportsController.FetchReportResult
                    {
                        Report = report,
                        AccessToken = embedToken.Generate(AccessKey)
                    });
                }

                return(new ReportsController.FetchReportResult
                {
                    Report = null,
                    AccessToken = null
                });
            }
        }
        public void CreateReportEmbedTokenWithRlsWithRolesAndMissingUsernameFails()
        {
            var workspaceId = Guid.NewGuid().ToString();
            var reportId    = Guid.NewGuid().ToString();

            PowerBIToken.CreateReportEmbedToken("Contoso", workspaceId, reportId, null, new[] { "TestRole" });
        }
        public void CanCreateReportEmbedToken()
        {
            var workspaceId = Guid.NewGuid().ToString();
            var reportId    = Guid.NewGuid().ToString();

            var token = PowerBIToken.CreateReportEmbedToken("Contoso", workspaceId, reportId, "TestUser", new [] { "TestRole" });

            Assert.IsNotNull(token);
            var jwt = token.Generate(this.accessKey);

            Assert.IsFalse(string.IsNullOrEmpty(jwt));

            var decodedToken = new JwtSecurityToken(jwt);

            var versionClaim  = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.Version);
            var wcnClaim      = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.WorkspaceCollectionName);
            var widClaim      = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.WorkspaceId);
            var ridCliam      = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.ReportId);
            var usernameClaim = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.Username);
            var rolesClaim    = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.Roles);

            Assert.AreEqual("PowerBISDK", decodedToken.Issuer);
            Assert.IsTrue(decodedToken.Audiences.Contains("https://analysis.windows.net/powerbi/api"));
            Assert.IsTrue(decodedToken.ValidTo >= DateTime.UtcNow);
            Assert.IsTrue(decodedToken.ValidTo <= DateTime.UtcNow.AddHours(1));
            Assert.AreEqual("0.2.0", versionClaim.Value);
            Assert.AreEqual("Contoso", wcnClaim.Value);
            Assert.AreEqual(workspaceId, widClaim.Value);
            Assert.AreEqual(reportId, ridCliam.Value);
            Assert.AreEqual("TestUser", usernameClaim.Value);
            Assert.AreEqual("TestRole", rolesClaim.Value);
        }
        public ActionResult Index(string reportId)
        {
            // use app key to create credentials used to generate embed tokens
            TokenCredentials credentials = new TokenCredentials(accessKey, "AppKey");
            PowerBIClient    client      = new PowerBIClient(credentials);

            // call to Power BI REST API to get list of reports inside a specific worksapce
            IList <Report> reports = client.Reports.GetReportsAsync(workspaceCollection, workspaceId).Result.Value;

            var viewModel = new ReportsViewModel {
                Reports = reports.ToList()
            };

            if (!string.IsNullOrEmpty(reportId))
            {
                Report       report     = reports.Where(r => r.Id == reportId).First();
                PowerBIToken embedToken = PowerBIToken.CreateReportEmbedToken(workspaceCollection, workspaceId, report.Id);
                viewModel.CurrentReport = new ReportViewModel {
                    Report      = report,
                    AccessToken = embedToken.Generate(accessKey)
                };
            }

            return(View(viewModel));
        }
Esempio n. 11
0
        public async Task <ActionResult> Index()
        {
            try
            {
                var devToken = PowerBIToken.CreateDevToken(this.workspaceCollection, this.workspaceId);
                using (var client = CreatePowerBIClient(devToken))
                {
                    var viewModel = new List <ReportViewModel>();

                    var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);

                    var dashboardReports = reportsResponse.Value.Where(m => m.Name.ToLower().Contains("dashboard"));

                    foreach (Report report in dashboardReports)
                    {
                        var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id);
                        viewModel.Add(new ReportViewModel()
                        {
                            Report = report, AccessToken = embedToken.Generate(this.accessKey)
                        });
                    }

                    return(View(viewModel));
                }
            }
            catch (Exception ex)
            {
                StorageClient.LogError(ex);
            }
            return(null);
        }
        public async Task <IHttpActionResult> SearchByName([FromUri] string query, [FromUri] bool includeTokens = false)
        {
            if (string.IsNullOrWhiteSpace(query))
            {
                return(Ok(Enumerable.Empty <ReportWithToken>()));
            }

            var credentials = new TokenCredentials(workspaceCollectionAccessKey, "AppKey");

            using (var client = new PowerBIClient(new Uri(apiUrl), credentials))
            {
                var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollectionName, this.workspaceId.ToString());

                var reports = reportsResponse.Value.Where(r => r.Name.ToLower().StartsWith(query.ToLower()));

                var reportsWithTokens = reports
                                        .Select(report =>
                {
                    string accessToken = null;
                    if (includeTokens)
                    {
                        var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollectionName, this.workspaceId.ToString(), report.Id);
                        accessToken    = embedToken.Generate(this.workspaceCollectionAccessKey);
                    }

                    return(new ReportWithToken(report, accessToken));
                })
                                        .ToList();

                return(Ok(reportsWithTokens));
            }
        }
        public async Task <IHttpActionResult> Get([FromUri] bool includeTokens = false)
        {
            var credentials = new TokenCredentials(workspaceCollectionAccessKey, "AppKey");

            using (var client = new PowerBIClient(new Uri(apiUrl), credentials))
            {
                var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollectionName, this.workspaceId.ToString());

                var reportsWithTokens = reportsResponse.Value
                                        .Select(report =>
                {
                    string accessToken = null;
                    if (includeTokens)
                    {
                        var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollectionName, this.workspaceId.ToString(), report.Id);
                        accessToken    = embedToken.Generate(this.workspaceCollectionAccessKey);
                    }

                    return(new ReportWithToken(report, accessToken));
                })
                                        .ToList();

                return(Ok(reportsWithTokens));
            }
        }
Esempio n. 14
0
        public async Task <ActionResult> Report(string reportId)
        {
            // Display specific PowerBi report

            using (var client = this.CreatePowerBIClient())
            {
                var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);

                var report = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);

                // Grab username and role
                var username = User.Identity.GetUserName();
                var roles    = Roles.GetRolesForUser(username);

                var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id, username, roles);

                var viewModel = new ReportViewModel
                {
                    Report      = report,
                    AccessToken = embedToken.Generate(this.accessKey)
                };

                return(View(viewModel)); // Need to create view
            }
        }
Esempio n. 15
0
        public void GetEmbedToken()
        {
            foreach (var report in powerbiReports)
            {
                PowerBIToken token = PowerBIToken.CreateReportEmbedToken(collectionName, currentWorkSpace.WorkspaceId, report.Id);
                var          jwt   = token.Generate(accessKey);

                Console.WriteLine("report {0}, access token: {1}", report.Name, jwt);
            }
        }
Esempio n. 16
0
        public IActionResult Index()
        {
            var embedToken  = PowerBIToken.CreateReportEmbedToken(_settings.WorkspaceCollection, _settings.WorkspaceId, "d3fa6537-83c1-4a9c-b165-c34e8448fd96", TimeSpan.FromDays(1.5));
            var accessToken = embedToken.Generate(_settings.AccessKey);

            return(View(new ReportViewModel()
            {
                EmbedToken = accessToken,
                ReportUrl = string.Concat("https://embedded.powerbi.com/appTokenReportEmbed?reportId=", "d3fa6537-83c1-4a9c-b165-c34e8448fd96")
                            //string.Concat(@"https://app.powerbi.com/reportEmbed?reportId=", "d3fa6537-83c1-4a9c-b165-c34e8448fd96")
            }));
        }
Esempio n. 17
0
        /// <summary>
        /// <a name="EmbedReportToken"/>
        /// ### Get an app token for a report
        /// </summary>
        protected void getReportAppTokenButton_Click(object sender, EventArgs e)
        {
            //Get an embed token for a report.
            var token = PowerBIToken.CreateReportEmbedToken(workspaceName, workspaceId, getReportAppTokenTextBox.Text);

            //After you get a PowerBIToken which has Claims including your WorkspaceName and WorkspaceID,
            //you generate JSON Web Token (JWT) . The Generate() method uses classes from System.IdentityModel.Tokens: SigningCredentials,
            //JwtSecurityToken, and JwtSecurityTokenHandler.
            string jwt = token.Generate(accessKey);

            //Set textbox to JWT string. The JavaScript embed code uses the embed jwt for a report.
            reportAppTokenTextbox.Text = jwt;
        }
Esempio n. 18
0
        public string Post([FromBody] PowerBITokenConstructor constructor)
        {
            var date   = DateTime.UtcNow.AddMinutes(120);
            var result = PowerBIToken.CreateReportEmbedToken(
                constructor.WorkspaceCollectionName,
                constructor.WorkspaceId,
                constructor.ReportId,
                constructor.DatasetId,
                date);

            var key = ConfigurationManager.AppSettings["PowerBIWorkspaceCollectionKey"];

            return(result.Generate(key));
        }
Esempio n. 19
0
        protected void renderReport()
        {
            using (var client = this.CreatePowerBIClient())
            {
                string myUserID        = selectedUser.Value.ToString();
                var    reportsResponse = client.Reports.GetReports(workspaceCollection, workspaceId);
                var    report          = reportsResponse.Value.FirstOrDefault();

                var    embedToken = (myUserID != "" ? PowerBIToken.CreateReportEmbedToken(workspaceCollection, workspaceId, report.Id, myUserID, new string[] { "SalesPerson" }) : PowerBIToken.CreateReportEmbedToken(workspaceCollection, workspaceId, report.Id, "adventure-works\\linda3", null));
                string myTok      = embedToken.Generate(accessKey);

                accessTokenText.Value = myTok;                                                                 //input on the report page.

                embedUrlText.Value = "https://embedded.powerbi.com/appTokenReportEmbed?reportId=" + report.Id; //input on the report page.
            }
        }
Esempio n. 20
0
        private string getReportToken(string reportId)
        {
            //Get an embed token for a report.
            var token = PowerBIToken.CreateReportEmbedToken(Settings.WorkspaceName, Settings.WorkspaceId, reportId);
            //var token = PowerBIToken.CreateReportEmbedToken(workspaceName, workspaceId, reportId);

            //After you get a PowerBIToken which has Claims including your WorkspaceName and WorkspaceID,
            //you generate JSON Web Token (JWT) . The Generate() method uses classes from System.IdentityModel.Tokens: SigningCredentials,
            //JwtSecurityToken, and JwtSecurityTokenHandler.
            string jwt = token.Generate(Settings.AccessKey);

            //string jwt = token.Generate(accessKey);

            //The JavaScript embed code uses the embed jwt for a report.
            return(jwt);
        }
        public void CanCreateReportEmbedTokenWithSlidingExpiration()
        {
            var workspaceId = Guid.NewGuid().ToString();
            var reportId    = Guid.NewGuid().ToString();

            var token = PowerBIToken.CreateReportEmbedToken("Contoso", workspaceId, reportId, TimeSpan.FromMinutes(2));

            Assert.IsNotNull(token);
            var jwt = token.Generate(this.accessKey);

            Assert.IsFalse(string.IsNullOrEmpty(jwt));

            var decodedToken = new JwtSecurityToken(jwt);

            Assert.IsTrue(decodedToken.ValidTo >= DateTime.UtcNow.AddMinutes(1));
            Assert.IsTrue(decodedToken.ValidTo <= DateTime.UtcNow.AddMinutes(2));
        }
Esempio n. 22
0
        public async Task <ActionResult> Report(string reportId)
        {
            ODataResponseListReport response;
            PowerBIToken            embedToken;
            Report      report;
            ReportModel reportModel;

            if (string.IsNullOrEmpty(reportId))
            {
                throw new ArgumentNullException(nameof(reportId));
            }

            try
            {
                using (IPowerBIClient client = GetPowerBIClient())
                {
                    response = await client.Reports.GetReportsAsync(
                        PowerBIConfig.WorkspaceCollectionName,
                        PowerBIConfig.WorkspaceId
                        );

                    report = response.Value.FirstOrDefault(r => r.Id == reportId);

                    embedToken = PowerBIToken.CreateReportEmbedToken(
                        PowerBIConfig.WorkspaceCollectionName,
                        PowerBIConfig.WorkspaceId,
                        report.Id
                        );

                    reportModel = new ReportModel()
                    {
                        AccessToken = embedToken.Generate(PowerBIConfig.AccessKey),
                        Report      = report
                    };

                    return(View(reportModel));
                }
            }
            finally
            {
                embedToken = null;
                report     = null;
                response   = null;
            }
        }
        public async Task <ActionResult> Report(string reportId)
        {
            // Display specific PowerBi report
            using (var client = this.CreatePowerBIClient())
            {
                var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);

                var report = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);

                // Grab username from ASP.NET identity
                var username = User.Identity.GetUserName();
                var userId   = User.Identity.GetUserId();

                var manager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext()));
                var user    = manager.FindById(userId);

                // Populate the PowerBiRole
                var powerBiRole = user.PowerBiRole;

                PowerBIToken embedToken;
                var          rowLevelSecurityEnabled = !string.IsNullOrEmpty(powerBiRole);

                if (rowLevelSecurityEnabled)
                {
                    // Convert single PowerBi role to collection as required by SDK
                    List <string> powerBiRoles = new List <string>();
                    powerBiRoles.Add(powerBiRole);

                    // By passing in Username and Roles we are allowing Row Level Security
                    embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id, username, powerBiRoles);
                }
                else
                {
                    embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id, username);
                }

                var viewModel = new ReportViewModel
                {
                    Report      = report,
                    AccessToken = embedToken.Generate(this.accessKey)
                };

                return(View(viewModel));
            }
        }
        public async Task <ActionResult> Report(string reportId)
        {
            using (var client = this.CreatePowerBIClient())
            {
                var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);

                var report     = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);
                var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id);

                var viewModel = new ReportViewModel
                {
                    Report      = report,
                    AccessToken = embedToken.Generate(this.accessKey)
                };

                return(View(viewModel));
            }
        }
Esempio n. 25
0
        public void CanCreateReportEmbedTokenWithScopes()
        {
            var workspaceId = Guid.NewGuid().ToString();
            var reportId    = Guid.NewGuid().ToString();
            var scopes      = Scopes.DatasetRead;

            var token = PowerBIToken.CreateReportEmbedToken("Contoso", workspaceId, reportId, scopes: scopes);

            Assert.IsNotNull(token);
            var jwt = token.Generate(this.accessKey);

            Assert.IsFalse(string.IsNullOrEmpty(jwt));

            var decodedToken = new JwtSecurityToken(jwt);

            var scopesClaim = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.Scopes).Value.Split(' ').ToList();

            Assert.IsTrue(scopesClaim.Contains(scopes));
        }
        static async Task GetEmbedInfo()
        {
            EnsureBasicParams(EnsureExtras.WorkspaceCollection | EnsureExtras.WorspaceId);
            int?index = -1;

            IList <Report> reports = await GetReports(workspaceCollectionName, workspaceId);

            if (reports == null || !reports.Any())
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("No report found in Workspace {0} from WorkspaceCollection {1}", workspaceId, workspaceCollectionName);
                return;
            }

            Console.WriteLine("Existing reports:");
            for (int i = 0; i < reports.Count; i++)
            {
                ConsoleHelper.WriteColoredStringLine(string.Format("{0} report name:{1}, Id:{2}", i + 1, reports[i].Name, reports[i].Id), ConsoleColor.Green, 2);
            }
            Console.WriteLine();

            index = userInput.EnsureIntParam(index, "Index of report to use (-1 for last in list)");
            if (!index.HasValue)
            {
                index = -1;
            }
            if (!index.HasValue || index.Value <= 0 || index.Value > reports.Count)
            {
                index = reports.Count;
            }

            Report report     = reports[index.Value - 1];
            var    embedToken = PowerBIToken.CreateReportEmbedToken(workspaceCollectionName, workspaceId, report.Id);

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Embed Url: {0}", report.EmbedUrl);
            Console.WriteLine("Embed Token: {0}", embedToken.Generate(accessKeys.Key1));
            var embedToken2 = PowerBIToken.CreateReportEmbedToken(workspaceCollectionName, workspaceId, report.Id);

            embedToken2.Claims.Add(new System.Security.Claims.Claim("type", "embed"));
            Console.WriteLine("Fixed Token: {0}", embedToken2.Generate(accessKeys.Key1));
        }
Esempio n. 27
0
        public async Task <ActionResult> Report(string reportId = "")
        {
            string tenantID   = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
            string reportName = Startup.tenantODataMap[tenantID][1];

            using (var client = this.CreatePowerBIClient())
            {
                var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);

                var report     = reportsResponse.Value.FirstOrDefault(r => (r.Id == reportId || r.Name == reportName));
                var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id);

                var viewModel = new ReportViewModel
                {
                    Report      = report,
                    AccessToken = embedToken.Generate(this.accessKey)
                };
                return(View(viewModel));
            }
        }
        public void CanCreateReportEmbedTokenWithRlsNoRoles()
        {
            var workspaceId = Guid.NewGuid().ToString();
            var reportId    = Guid.NewGuid().ToString();

            var token = PowerBIToken.CreateReportEmbedToken("Contoso", workspaceId, reportId, "TestUser");

            Assert.IsNotNull(token);
            var jwt = token.Generate(this.accessKey);

            Assert.IsFalse(string.IsNullOrEmpty(jwt));

            var decodedToken = new JwtSecurityToken(jwt);

            var usernameClaim = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.Username);
            var rolesClaim    = decodedToken.Claims.FirstOrDefault(c => c.Type == PowerBIToken.ClaimTypes.Roles);

            Assert.AreEqual("TestUser", usernameClaim.Value);
            Assert.IsNull(rolesClaim);
        }
        public async Task <ActionResult> Report(string reportId)
        {
            using (var client = this.CreatePowerBIClient())
            {
                string myUserID             = User.Identity.Name.ToString();
                IEnumerable <string> myRole = new List <string>()
                {
                    "Customer", "Developer"
                };
                var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);

                var report     = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);
                var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, report.Id, myUserID, myRole);
                var viewModel  = new ReportViewModel
                {
                    Report      = report,
                    AccessToken = embedToken.Generate(this.accessKey)
                };
                return(View(viewModel));
            }
        }
        public async Task <ActionResult> Report()
        {
            ClaimsIdentity claimsId = ClaimsPrincipal.Current.Identity as ClaimsIdentity;
            var            appRoles = claimsId.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value).ToList();
            string         reportId = Request.Form["SelectedReport"].ToString();

            using (var client = this.CreatePowerBIClient())
            {
                var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId);

                var report     = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);
                var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId, reportId, username: "******", roles: appRoles);

                var viewModel = new ReportViewModel
                {
                    Report      = report,
                    AccessToken = embedToken.Generate(this.accessKey)
                };
                return(View(viewModel));
            }
        }