I have a QueryInterceptor that looks like the below. Basically, all it is doing is getting the current AppUserEntity and using that in the Filter. This all works fine until I put the [RequiresAuthentication] attribute on the AppUserEntity. When I do that, I get an EntityServerError that says : Parameter 'principal' cannot be null. The error occurs during the execution of AppUserEntity query.
public class QueryInterceptor : EntityServerQueryInterceptor
{
protected override bool FilterQuery()
{
if (this.Query.ElementType == typeof(EmailAccountEntity))
{
var currentUser = this.GetCurrentAppUserEntity();
if (currentUser != null)
{
QueryFilters.AddFilter<EmailAccountEntity>(q => q.Where(c => c.OwnedBy == currentUser.Id));
}
else
{
//Will return no records.
QueryFilters.AddFilter<EmailAccountEntity>(q => q.Where(c => c.OwnedBy == -1));
}
}
return base.FilterQuery();
}
protected AppUserEntity GetCurrentAppUserEntity()
{
string name = this.Principal.Identity.Name;
IEntityQuery<AppUserEntity> query =
this.EntityManager.GetQuery<AppUserEntity>()
.Where(user => user.AspNetDbUserName == name );
var appUsers = this.EntityManager.ExecuteQuery<AppUserEntity>(query); //This is where the error occurs.
return appUsers.FirstOrDefault();
}
}
I like having the [RequiresAuthentication] attribute because it ensure that no-one is going to be getting to those entities without being logged in. I could manually do this in the QueryInterceptor, but would prefer the attribute as it's less error prone.
I did some testing and any Entity type that is queried in the QueryInterceptor throws the same error if the RequiresAuthentication attribute is used on it.
Greg