Errors using a custom RIA authentication service (and how to resolve them)

If you've created your own authentication service for a Silverlight RIA application, you might encounter the following scenarios:

1) If you get the following errors:

  • GetUser should have returned a single user
  • Logout should have returned a single, anonymous user

Then the chances are your implementation of GetDefaultUser is returning null.

2) However, if you get these:

  • Load operation failed for query 'GetUser'. Entity 'User : null' cannot be added to cache because it doesn't have a valid identity
  • Load operation failed for query 'Logout'. Entity 'User : null' cannot be added to cache because it doesn't have a valid identity

Then you're probably returning a user object that has a null Name.

Fixing either of these problems is pretty simple - just make sure you return a populated user from GetDefaultUser that has a name of String.Empty. Although the property IsAnonymous will return true if you set the name to null, other parts of the framework in the Silverlight client will blow up if you try this, hence scenario 2.


  • Waleed said

    Dear Mike, I hope you can help me fix my "load operation fail" I have the User DefaultUser as follows: private static User DefaultUser = new User() { Admin = 0, Name = string.Empty, Roles = null, Password = string.Empty, UserName = string.Empty, }; and also the GetUser() as follows: public User GetUser() { if ((this.ServiceContext != null) && (this.ServiceContext.User != null) && this.ServiceContext.User.Identity.IsAuthenticated) { return this.GetUser(this.ServiceContext.User.Identity.Name); } return DefaultUser; } The implementation of the GetUser() as it return the value from database: private User GetUser(string userName) { User user = new User(); user li = this.ObjectContext.users.First(u => u.user_name == userName); user.Admin = li.admin; user.UserName = li.user_name; user.Password = li.password; return user; } Everything is fine (no NULL's) but I still get this error when I try to call the LoadUser from the webcontext as follows: WebContext.Current.Authentication.LoadUser(OnLoadUser_Completed, null); appreciate If you can help... Regards

  • Bustin said

    Thanks a lot! This helped me get to my real issue. The principal.Identity.Name contained the domain name as well, my DoesUserExist override from my forms authentication was not accounting for this. Saved me hours of intimate head desk time.

Comments have been disabled for this content.