Forum Discussion
Embedding with RLS: NulLReferenceException when Generating Token
It seems the document is a little out of date.
There is no need to replace the code for the
"// Generate Embed Token for reports without effective identities."
part.
The code you posted already have the RLS defined within it.
GenerateTokenRequest generateTokenRequestParameters;
// This is how you create embed token with effective identities
if (!string.IsNullOrEmpty(username))
{
var rls = new EffectiveIdentity(username, new List<string> { report.DatasetId });
if (!string.IsNullOrWhiteSpace(roles))
{
var rolesList = new List<string>();
rolesList.AddRange(roles.Split(','));
rls.Roles = rolesList;
}
// Generate Embed Token with effective identities.
generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view", identities: new List<EffectiveIdentity> { rls });
}
else
{
// Generate Embed Token for reports without effective identities.
generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
}The else part would then generate token without effective identities, which means the username (UserId) parameter is empty.
What you need is to verify that you have the proper parameter passed into the function.
Input the UserName part as User Email Address, see if it would work.
The testing results from my side:
The logic in the code should be:
1. Check the userName field, if it is empty, load embedtoken without RLS,
2. If UserName is not Empty, check if there are roles inputted, if yes, load the RLS, otherwise, without RLS.
Regards,
Michael
I am still getting the same error. Relevant code:
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<string> { report.DatasetId });
if (!string.IsNullOrWhiteSpace(roles))
{
var rolesList = new List<string>();
rolesList.AddRange(roles.Split(','));
rls.Roles = rolesList;
}
// Generate Embed Token with effective identities.
generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view", identities: new List<EffectiveIdentity> { rls });
}
else
{
// Generate Embed Token for reports without effective identities.
generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
}I should note that I'm using the UserID (Unique Identifier), not the email address. Power BI Desktop allows me to view as roles using the UserID.
- ats19588 years agoHelper II
I also tried adding the datasetId as follows:
var rls = new EffectiveIdentity(username, new List<string> { "12dac33c-af76-4e1b-b064-57d837679b0c" });