<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: RLS LIVE CONNECTION DATASET in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/RLS-LIVE-CONNECTION-DATASET/m-p/1679917#M28002</link>
    <description>&lt;P&gt;Hi &lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/173530"&gt;@santiagomur&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Based on your description, could you use this dataset as a shared dataset and Other reports are then created based on this shared dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best Regards,&lt;BR /&gt;Liang&lt;BR /&gt;If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 22 Feb 2021 06:15:20 GMT</pubDate>
    <dc:creator>V-lianl-msft</dc:creator>
    <dc:date>2021-02-22T06:15:20Z</dc:date>
    <item>
      <title>RLS LIVE CONNECTION DATASET</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/RLS-LIVE-CONNECTION-DATASET/m-p/1674231#M27933</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i have a problem with the config&amp;nbsp; of the rls, i already use rls and it works fine, but now the plan is to make 1 dataset and many reports, i try to use the same config that i use when is 1 report per dataset and doesnt works, i ont know if i need to make some changes on the config.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the config is this&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="css"&gt;  #r "System.Web.Extensions"  
    using System.Configuration;  
    using System.Net;  
    using System.Text;  
    using System.Web.Script.Serialization;  
    using Microsoft.IdentityModel.Clients.ActiveDirectory;  
    using Microsoft.PowerBI.Api.V2;  
    using Microsoft.PowerBI.Api.V2.Models;  
    using Microsoft.Rest;  
      
    // Static Values      
    static string authorityUrl = ConfigurationManager.AppSettings["AUTHORITY_URL"];    
    static string resourceUrl = ConfigurationManager.AppSettings["RESOURCE_URL"];      
    static string apiUrl = ConfigurationManager.AppSettings["POWERBI_API_URL"];  
    static string clientId = ConfigurationManager.AppSettings["PBIE_CLIENT_ID"];
    static string username = ConfigurationManager.AppSettings["PBIE_USERNAME"];
    static string password = ConfigurationManager.AppSettings["PBIE_PASSWORD"];      
    static string defaultUser = "";
    static string defaultRol = "USUARIO";
    static string defaultReport = "";
    static string defaultGroup = "";
    static string defaultDashboard = "";
    static string defaultDataset = "";
      
    public static async Task&amp;lt;HttpResponseMessage&amp;gt; Run(HttpRequestMessage req, TraceWriter log)  
    {  
		log.Info( string.Format("Dump request:\n {0}",req.ToString()));
        
        PostData userData = new PostData();

        //Recover req PostData
        PostData jsonData = await req.Content.ReadAsAsync&amp;lt;PostData&amp;gt;();
        if (jsonData != null){
           log.Info ("JsonReq: "+ jsonData.ToString());
           userData = prepareUserData(jsonData);
        } else {
            //Default values
            userData = prepareUserData(new PostData());
        }
        log.Info("userData: " + userData.ToString());


        // Authenticate with Azure Ad &amp;gt; Get Access Token &amp;gt; Get Token Credentials  
        var credential = new UserPasswordCredential(username, password);  

        var authenticationContext = new AuthenticationContext(authorityUrl);  
        var authenticationResult = await authenticationContext.AcquireTokenAsync(resourceUrl, clientId, credential);  
        string accessToken = authenticationResult.AccessToken;  
        var tokenCredentials = new TokenCredentials(accessToken, "Bearer"); 
        log.Info ("Llega");
        // PowerBI method  
        using (var client = new PowerBIClient(new Uri(apiUrl), tokenCredentials))  
        {  
            // Embed URL
            GenerateTokenRequest generateTokenRequestParameters;
            generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");  
            string embedUrl;


            /////////////////////////////// DASHBOARD ///////////////////////////////
            if (!string.IsNullOrWhiteSpace(userData.dashboardId)) 
            {
                embedUrl = "https://app.powerbi.com/dashboardEmbed";
                log.Info ("Llega1");
                var tokenResponse = await client.Dashboards.GenerateTokenInGroupAsync(userData.groupId, userData.dashboardId, generateTokenRequestParameters);
                
                // JSON Response  
                EmbedContent data = new EmbedContent();  
                data.EmbedToken = tokenResponse.Token;  
                data.EmbedUrl = embedUrl;  
                data.ReportId = userData.dashboardId;
                data.DashboardId = userData.dashboardId;

                JavaScriptSerializer js = new JavaScriptSerializer();  
                string jsonp = "callback(" +  js.Serialize(data) + ");";
                log.Info(jsonp);

                // Return Response  
                return new HttpResponseMessage(HttpStatusCode.OK)   
                {  
                    Content = new StringContent(jsonp, Encoding.UTF8, "application/json")  
                }; 
            }

            else
            /////////////////////////////// REPORT ///////////////////////////////
            {
                embedUrl = "https://app.powerbi.com/reportEmbed";
                log.Info ("Llega2");
                Report report = client.Reports.GetReportInGroup(userData.groupId, userData.reportId);  
                var result = new EmbedConfig();
                log.Info(embedUrl);
                
                string allParameter = String.Concat(userData.userID,";",userData.companyY2MX,";",userData.environment,";",userData.mediatorCode,";",userData.commercialCode); 
                log.Info("allParameter: " + allParameter);
                
                result = new EmbedConfig { Username = allParameter, Roles = userData.roles};
            
                var datasets = await client.Datasets.GetDatasetByIdInGroupAsync(userData.groupId, report.DatasetId);
                log.Info("DataSet: " + report.DatasetId);
                result.IsEffectiveIdentityRequired = datasets.IsEffectiveIdentityRequired;
                result.IsEffectiveIdentityRolesRequired = datasets.IsEffectiveIdentityRolesRequired;
                log.Info("Dataset IsEffectiveIdentityRequired: " + result.IsEffectiveIdentityRequired);
                log.Info("Dataset IsEffectiveIdentityRolesRequired: " + result.IsEffectiveIdentityRolesRequired);

                //GenerateTokenRequest generateTokenRequestParameters;
                log.Info ("Llega3");
                if (!string.IsNullOrWhiteSpace(userData.userID))
                {
                    if (result.IsEffectiveIdentityRolesRequired==true)
                    {   
                        var rls = new EffectiveIdentity(allParameter, new List&amp;lt;string&amp;gt; {  report.DatasetId });
                        if (!string.IsNullOrWhiteSpace(userData.roles))
                        {
                            var rolesList = new List&amp;lt;string&amp;gt;();
                            rolesList.AddRange(userData.roles.Split(','));
                                            
                            rls.Roles = rolesList;                        
                        }
                        // Generate Embed Token with effective identities.
                        generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view", identities: new List&amp;lt;EffectiveIdentity&amp;gt; { rls });
                        log.Info("Token Request with EffectiveIdentity");
                        }
                    else
                    {
                        // Generate Embed Token for reports without effective identities.
                        generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
                        log.Info("Token Request without EffectiveIdentity");
                    }
                }
                else
                {
                    // Generate Embed Token for reports without effective identities.
                    generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
                    log.Info("Token Request without EffectiveIdentity");
                }
                log.Info("Token Request " + userData);
                log.Info("generateTokenRequestParameters " + generateTokenRequestParameters);



                var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(userData.groupId, userData.reportId, generateTokenRequestParameters);


                // JSON Response  
                EmbedContent data = new EmbedContent();  
                data.EmbedToken = tokenResponse.Token;  
                data.EmbedUrl = embedUrl;
                data.ReportId = userData.reportId;
                data.DashboardId = userData.reportId;
                
                JavaScriptSerializer js = new JavaScriptSerializer();  
                string jsonp = "callback(" +  js.Serialize(data) + ");";  
                log.Info(jsonp);

                // Return Response  
                return new HttpResponseMessage(HttpStatusCode.OK)   
                {  
                    Content = new StringContent(jsonp, Encoding.UTF8, "application/json")  
                }; 
            }
        }  
    } 

    public static PostData prepareUserData(PostData data) {
        PostData userData = new PostData();  

         if (!string.IsNullOrWhiteSpace(data.userID)){
            userData.userID = data.userID;
         } else {
             userData.userID = defaultUser;
         }
         if (!string.IsNullOrWhiteSpace(data.roles)){
            userData.roles = data.roles;
         } else {
             userData.roles = defaultRol;
         }
         if (!string.IsNullOrWhiteSpace(data.reportId)){
            userData.reportId = data.reportId;
         } else {
             userData.reportId = defaultReport;
         }
         if (!string.IsNullOrWhiteSpace(data.groupId)){
            userData.groupId = data.groupId;
         } else {
             userData.groupId = defaultGroup;
         }
         if (!string.IsNullOrWhiteSpace(data.dashboardId)){
            userData.dashboardId = data.dashboardId;
         } else {
             userData.dashboardId = defaultDashboard;
         }

         if (!string.IsNullOrWhiteSpace(data.environment)){
            userData.environment = data.environment;
         } else {
             userData.environment = "4";
         }
         if (!string.IsNullOrWhiteSpace(data.companyY2MX)){
            userData.companyY2MX = data.companyY2MX;
         } else {
             userData.companyY2MX = "G";
         }
         if (!string.IsNullOrWhiteSpace(data.mediatorCode)){
            userData.mediatorCode = data.mediatorCode;
         } else {
             userData.mediatorCode = "0";
         }
         if (!string.IsNullOrWhiteSpace(data.commercialCode)){
            userData.commercialCode = data.commercialCode;
         } else {
             userData.commercialCode = "0";
         }
          if (!string.IsNullOrWhiteSpace(data.datasetId)){
            userData.datasetId = data.datasetId;
         } else {
             userData.datasetId = defaultDataset;
         }
           
         return userData;
    }

    //Json Response Class  
    public class EmbedContent  
    {  
        public string EmbedToken { get; set; }  
        public string EmbedUrl { get; set; }  
        public string ReportId { get; set; }  
        public string DashboardId { get; set; }
    }

    //Json Request Post Class.
    public class PostData
    {
        public string userID { get;set; }
        public string environment { get;set; }
        public string roles { get;set; }
        public string reportId { get;set; }
        public string groupId { get;set; }        
        public string companyY2MX { get;set; }
        public string mediatorCode { get;set; }
        public string commercialCode { get;set; }
        public string dashboardId { get;set; }
        public string datasetId { get;set; }

        public override string ToString()
        {
            return "userID : " + userID + 
            ", environment: " + environment +
            ", roles: " + roles +
            ", reportId: " + reportId +
            ", groupId: " + groupId +
            ", dashboardId: " + dashboardId +
            ", cia: " + companyY2MX +
            ", mediatorCode: " + mediatorCode +
            ", commercialCode: " + commercialCode +
            ", datasetId: " + datasetId;
        }
    }  


    public class EmbedConfig
    {
        public string Id { get; set; }
        public string EmbedUrl { get; set; }
        public EmbedToken EmbedToken { get; set; }
        public int MinutesToExpiration
        {
            get
            {
                var minutesToExpiration = EmbedToken.Expiration.Value - DateTime.UtcNow;
                return minutesToExpiration.Minutes;
            }
        }
        public bool? IsEffectiveIdentityRolesRequired { get; set; }
        public bool? IsEffectiveIdentityRequired { get; set; }
        public bool EnableRLS { get; set; }
        public string Username { get; set; }
        public string Roles { get; set; }
        public string ErrorMessage { get; internal set; }
    }
    &lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 18 Feb 2021 09:34:39 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/RLS-LIVE-CONNECTION-DATASET/m-p/1674231#M27933</guid>
      <dc:creator>santiagomur</dc:creator>
      <dc:date>2021-02-18T09:34:39Z</dc:date>
    </item>
    <item>
      <title>Re: RLS LIVE CONNECTION DATASET</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/RLS-LIVE-CONNECTION-DATASET/m-p/1679917#M28002</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/173530"&gt;@santiagomur&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Based on your description, could you use this dataset as a shared dataset and Other reports are then created based on this shared dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best Regards,&lt;BR /&gt;Liang&lt;BR /&gt;If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Feb 2021 06:15:20 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/RLS-LIVE-CONNECTION-DATASET/m-p/1679917#M28002</guid>
      <dc:creator>V-lianl-msft</dc:creator>
      <dc:date>2021-02-22T06:15:20Z</dc:date>
    </item>
  </channel>
</rss>

