<?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: Embedding with RLS: NulLReferenceException when Generating Token in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/414726#M12349</link>
    <description>&lt;P&gt;I am still getting the same error. Relevant code:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;username = System.Web.HttpContext.Current.User.Identity.GetUserId();
                    roles = "allusers";
                    var datasets = await client.Datasets.GetDatasetByIdInGroupAsync(GroupId, report.DatasetId);
                    result.IsEffectiveIdentityRequired = datasets.IsEffectiveIdentityRequired;
                    result.IsEffectiveIdentityRolesRequired = datasets.IsEffectiveIdentityRolesRequired;
                    GenerateTokenRequest generateTokenRequestParameters;
                    // This is how you create embed token with effective identities
                    
                    if (!string.IsNullOrEmpty(username))
                    {
                        var rls = new EffectiveIdentity(username, new List&amp;lt;string&amp;gt; { report.DatasetId });
                        if (!string.IsNullOrWhiteSpace(roles))
                        {
                            var rolesList = new List&amp;lt;string&amp;gt;();
                            rolesList.AddRange(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 });
                    }
                    else
                    {
                        // Generate Embed Token for reports without effective identities.
                        generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
                    }&lt;/PRE&gt;&lt;P&gt;I should note that I'm using the UserID (Unique Identifier), not the email address.&amp;nbsp;Power BI Desktop allows me to view as roles using the UserID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 10 May 2018 13:41:51 GMT</pubDate>
    <dc:creator>ats1958</dc:creator>
    <dc:date>2018-05-10T13:41:51Z</dc:date>
    <item>
      <title>Embedding with RLS: NulLReferenceException when Generating Token</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/409975#M12222</link>
      <description>&lt;P&gt;I am getting a nullreferenceexception on the line&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;FONT color="red"&gt;var accessToken = "@Model.EmbedToken.Token";&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The report was rendering properly before attempting RLS. Here's my controller:&lt;/P&gt;&lt;PRE&gt;else&lt;BR /&gt; {&lt;BR /&gt; // Generate Embed Token for reports without effective identities.&lt;BR /&gt;generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view", identities: new List&amp;lt;EffectiveIdentity&amp;gt; { new EffectiveIdentity(username: System.Web.HttpContext.Current.User.Identity.GetUserId(), roles: new List&amp;lt;string&amp;gt; { "allusers" }, datasets: new List&amp;lt;string&amp;gt; { "12dac33c-af76-4e1b-b064-57d837679b0c" }) });&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt; var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, report.Id, generateTokenRequestParameters);&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 May 2018 13:53:25 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/409975#M12222</guid>
      <dc:creator>ats1958</dc:creator>
      <dc:date>2018-05-03T13:53:25Z</dc:date>
    </item>
    <item>
      <title>Re: Embedding with RLS: NulLReferenceException when Generating Token</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/411092#M12257</link>
      <description>&lt;P&gt;Where&amp;nbsp;did you implement the controller?&lt;/P&gt;&lt;P&gt;By the way, could you please share the full code for the generateToken part?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you are using the App owns Data sample, make sure you have followed the steps mentioned in the article below:&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.microsoft.com/en-us/power-bi/developer/embedded-row-level-security" target="_blank"&gt;https://docs.microsoft.com/en-us/power-bi/developer/embedded-row-level-security&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just replace:&lt;/P&gt;&lt;PRE&gt;// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");

var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, report.Id, generateTokenRequestParameters);&lt;/PRE&gt;&lt;P&gt;with the code below:&lt;/P&gt;&lt;PRE&gt;var generateTokenRequestParameters = new GenerateTokenRequest("View", null, identities: new List&amp;lt;EffectiveIdentity&amp;gt; { new EffectiveIdentity(username: "username", roles: new List&amp;lt;string&amp;gt; { "roleA", "roleB" }, datasets: new List&amp;lt;string&amp;gt; { "datasetId" }) });

var tokenResponse = await client.Reports.GenerateTokenInGroupAsync("groupId", "reportId", generateTokenRequestParameters);&lt;/PRE&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Michael&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 May 2018 01:37:55 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/411092#M12257</guid>
      <dc:creator>v-micsh-msft</dc:creator>
      <dc:date>2018-05-05T01:37:55Z</dc:date>
    </item>
    <item>
      <title>Re: Embedding with RLS: NulLReferenceException when Generating Token</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/411437#M12264</link>
      <description>&lt;P&gt;I'm not sure what you mean by where the controller is implemented. But my full controller code is below. The only thing that have changed are the relevant lines which I already posted.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did follow the steps in the article you attached.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; [Authorize]
        public async Task&amp;lt;ActionResult&amp;gt; Index(string username, string roles)
        {
            var result = new EmbedConfig();
            try
            {
                result = new EmbedConfig { Username = username, Roles = roles };
                var error = GetWebConfigErrors();
                if (error != null)
                {
                    result.ErrorMessage = error;
                    return View(result);
                }

                // Create a user password cradentials.
                var credential = new UserPasswordCredential(Username, Password);

                // Authenticate using created credentials
                var authenticationContext = new AuthenticationContext(AuthorityUrl);
                var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);

                if (authenticationResult == null)
                {
                    result.ErrorMessage = "Authentication Failed.";
                    return View(result);
                }

                var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");

                // Create a Power BI Client object. It will be used to call Power BI APIs.
                using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
                {
                    // Get a list of reports.
                    var reports = await client.Reports.GetReportsInGroupAsync(GroupId);

                    Report report;
                    if (string.IsNullOrEmpty(ReportId))
                    {
                        // Get the first report in the group.
                        report = reports.Value.FirstOrDefault();
                    }
                    else
                    {
                        report = reports.Value.FirstOrDefault(r =&amp;gt; r.Id == ReportId);
                    }

                    if (report == null)
                    {
                        result.ErrorMessage = "Group has no reports.";
                        return View(result);
                    }

                    var datasets = await client.Datasets.GetDatasetByIdInGroupAsync(GroupId, report.DatasetId);
                    result.IsEffectiveIdentityRequired = datasets.IsEffectiveIdentityRequired;
                    result.IsEffectiveIdentityRolesRequired = datasets.IsEffectiveIdentityRolesRequired;
                    GenerateTokenRequest generateTokenRequestParameters;
                    // This is how you create embed token with effective identities
                    if (!string.IsNullOrEmpty(username))
                    {
                        var rls = new EffectiveIdentity(username, new List&amp;lt;string&amp;gt; { report.DatasetId });
                        if (!string.IsNullOrWhiteSpace(roles))
                        {
                            var rolesList = new List&amp;lt;string&amp;gt;();
                            rolesList.AddRange(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 });
                    }
                    else
                    {
                        // Generate Embed Token for reports without effective identities.
                        generateTokenRequestParameters = new GenerateTokenRequest("View", null, identities: new List&amp;lt;EffectiveIdentity&amp;gt; { new EffectiveIdentity(username: System.Web.HttpContext.Current.User.Identity.GetUserId(), roles: new List&amp;lt;string&amp;gt; { "allusers" }, datasets: new List&amp;lt;string&amp;gt; { "12dac33c-af76-4e1b-b064-57d837679b0c" }) });
                    }

                    var tokenResponse = await client.Reports.GenerateTokenInGroupAsync("groupId", "reportId", generateTokenRequestParameters);

                    if (tokenResponse == null)
                    {
                        result.ErrorMessage = "Failed to generate embed token.";
                        return View(result);
                    }

                    // Generate Embed Configuration.
                    result.EmbedToken = tokenResponse;
                    result.EmbedUrl = report.EmbedUrl;
                    result.Id = report.Id;

                    return View(result);
                }
            }
            catch (HttpOperationException exc)
            {
                result.ErrorMessage = string.Format("Status: {0} ({1})\r\nResponse: {2}\r\nRequestId: {3}", exc.Response.StatusCode, (int)exc.Response.StatusCode, exc.Response.Content, exc.Response.Headers["RequestId"].FirstOrDefault());
            }
            catch (Exception exc)
            {
                result.ErrorMessage = exc.ToString();
            }

            return View(result);
        }&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 May 2018 23:37:14 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/411437#M12264</guid>
      <dc:creator>ats1958</dc:creator>
      <dc:date>2018-05-06T23:37:14Z</dc:date>
    </item>
    <item>
      <title>Re: Embedding with RLS: NulLReferenceException when Generating Token</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/412109#M12282</link>
      <description>&lt;P&gt;I doubt this is the issue, but I'm using a Guid as the username. Perhaps the length of that is the issue. Here's a sample one:&lt;/P&gt;&lt;P&gt;845cc21b-eeca-49a6-b2b7-17a8ac09de7f&lt;/P&gt;</description>
      <pubDate>Mon, 07 May 2018 17:42:01 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/412109#M12282</guid>
      <dc:creator>ats1958</dc:creator>
      <dc:date>2018-05-07T17:42:01Z</dc:date>
    </item>
    <item>
      <title>Re: Embedding with RLS: NulLReferenceException when Generating Token</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/414251#M12331</link>
      <description>&lt;P&gt;It seems the document is a little out of date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is no need to replace the code for the&amp;nbsp;&lt;/P&gt;&lt;P&gt;"// Generate Embed Token for reports without effective identities."&lt;/P&gt;&lt;P&gt;part.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code you posted already have the RLS defined within it.&lt;/P&gt;&lt;PRE&gt;GenerateTokenRequest generateTokenRequestParameters;
                    // This is how you create embed token with effective identities
                    &lt;STRONG&gt;if (!string.IsNullOrEmpty(username))
                    {
                        var rls = new EffectiveIdentity(username, new List&amp;lt;string&amp;gt; { report.DatasetId });
                        if (!string.IsNullOrWhiteSpace(roles))
                        {
                            var rolesList = new List&amp;lt;string&amp;gt;();
                            rolesList.AddRange(roles.Split(','));
                            rls.Roles = rolesList;
                        }&lt;/STRONG&gt;
                        &lt;STRONG&gt;// Generate Embed Token with effective identities.
                        generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view", identities: new List&amp;lt;EffectiveIdentity&amp;gt; { rls });
                    }&lt;/STRONG&gt;
                   &lt;STRONG&gt; else&lt;/STRONG&gt;
                    {
                        // Generate Embed Token for reports without effective identities.
                        generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
                    }&lt;/PRE&gt;&lt;P&gt;The else part would then generate token without effective identities, which means the&amp;nbsp;username (UserId) parameter is empty.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What you need is to verify that you have the proper parameter passed into the function.&lt;/P&gt;&lt;P&gt;Input the UserName part as User Email Address, see if it would work.&lt;/P&gt;&lt;P&gt;The testing results from my side:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="38.PNG" style="width: 600px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/95232iA7B6CADAF47DC966/image-size/large?v=v2&amp;amp;px=999" role="button" title="38.PNG" alt="38.PNG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="39.PNG" style="width: 600px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/95233iF804427211B1BF31/image-size/large?v=v2&amp;amp;px=999" role="button" title="39.PNG" alt="39.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The logic in the code should be:&lt;/P&gt;&lt;P&gt;1. Check the userName field, if it is empty, load embedtoken without RLS,&lt;/P&gt;&lt;P&gt;2. If UserName is not Empty, check if there are roles inputted, if yes, load the RLS, otherwise, without RLS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Michael&lt;/P&gt;</description>
      <pubDate>Thu, 10 May 2018 03:19:00 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/414251#M12331</guid>
      <dc:creator>v-micsh-msft</dc:creator>
      <dc:date>2018-05-10T03:19:00Z</dc:date>
    </item>
    <item>
      <title>Re: Embedding with RLS: NulLReferenceException when Generating Token</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/414726#M12349</link>
      <description>&lt;P&gt;I am still getting the same error. Relevant code:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;username = System.Web.HttpContext.Current.User.Identity.GetUserId();
                    roles = "allusers";
                    var datasets = await client.Datasets.GetDatasetByIdInGroupAsync(GroupId, report.DatasetId);
                    result.IsEffectiveIdentityRequired = datasets.IsEffectiveIdentityRequired;
                    result.IsEffectiveIdentityRolesRequired = datasets.IsEffectiveIdentityRolesRequired;
                    GenerateTokenRequest generateTokenRequestParameters;
                    // This is how you create embed token with effective identities
                    
                    if (!string.IsNullOrEmpty(username))
                    {
                        var rls = new EffectiveIdentity(username, new List&amp;lt;string&amp;gt; { report.DatasetId });
                        if (!string.IsNullOrWhiteSpace(roles))
                        {
                            var rolesList = new List&amp;lt;string&amp;gt;();
                            rolesList.AddRange(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 });
                    }
                    else
                    {
                        // Generate Embed Token for reports without effective identities.
                        generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
                    }&lt;/PRE&gt;&lt;P&gt;I should note that I'm using the UserID (Unique Identifier), not the email address.&amp;nbsp;Power BI Desktop allows me to view as roles using the UserID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 May 2018 13:41:51 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/414726#M12349</guid>
      <dc:creator>ats1958</dc:creator>
      <dc:date>2018-05-10T13:41:51Z</dc:date>
    </item>
    <item>
      <title>Re: Embedding with RLS: NulLReferenceException when Generating Token</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/414736#M12351</link>
      <description>&lt;P&gt;I also tried adding the datasetId as follows:&lt;/P&gt;&lt;PRE&gt;var rls = new EffectiveIdentity(username, new List&amp;lt;string&amp;gt; { "12dac33c-af76-4e1b-b064-57d837679b0c" });&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 May 2018 13:52:00 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Embedding-with-RLS-NulLReferenceException-when-Generating-Token/m-p/414736#M12351</guid>
      <dc:creator>ats1958</dc:creator>
      <dc:date>2018-05-10T13:52:00Z</dc:date>
    </item>
  </channel>
</rss>

