Print Page | Close Window

QueryInterceptor exception

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2644
Printed Date: 02-Jul-2026 at 1:28pm


Topic: QueryInterceptor exception
Posted By: gregweb
Subject: QueryInterceptor exception
Date Posted: 28-Apr-2011 at 8:47am
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



Replies:
Posted By: sbelini
Date Posted: 02-May-2011 at 5:29pm
Hi Greg,
 
I was able to reproduce the issue here.
 
I am filing a bug on this and will keep you updated on its status.
 
In the meantime a workaround would be checking if Principal is authenticated instead of setting the [RequiresAuthentication] attribute:
 
protected Employee GetCurrentAppUserEntity() {
  string name = this.Principal.Identity.Name;
  if (this.Principal.Identity.IsAuthenticated) {
    IEntityQuery<Employee> query =
      this.EntityManager.GetQuery<Employee>()
      .Where(user => user.FirstName == name);
    var appUsers = this.EntityManager.ExecuteQuery<Employee>(query);
    return appUsers.FirstOrDefault();
  }
  return null;
}
 
Regards,
   Silvio.


Posted By: gregweb
Date Posted: 03-May-2011 at 8:22am
Great, thanks very much.
 
Greg


Posted By: sbelini
Date Posted: 09-Jun-2011 at 11:16am
Hi Greg,
 
I want to let you know that this issue has been fixed and will be available in DevForce 6.1.1.0. (coming up shortly)
 
Silvio.


Posted By: gregweb
Date Posted: 09-Jun-2011 at 12:45pm
Looking forward to it.
 
Greg



Print Page | Close Window