Friday, 12 July 2013

ASP.NET Universal Providers role management "feature"

Many moons ago I wrote a WPF application for managing the ASP.NET provider store.  I need a credential manager for the test team on a project I'm currently working on that uses the "new" Universal Providers.  So I assumed I could just dust off the old code, change the config a little and life would be good.
As with all good intentions, it didn't take me long to get to hell.  Specifically the following methods kept failing with some meaningless guff about object reference not being set to an instance etc.:
  • Roles.GetRolesForUser
  • Roles.IsUserInRole
I ran a SQL Profiler trace to make sure it wasn't trying to use the wrong provider, but it was obvious that it wasn't even hitting the data store.  The Roles object is static, so the method pointers should never be null.  This was obviously an issue with the provider.
My original code was:
var roles = Roles.GetRolesForUser(userName);
To cut a long story short, I called the method on the Provider property of the Roles object like so:
var provider = Roles.Provider;
Debug.Assert(provider != null);
var roles = provider.GetRolesForUser(userName);
Now all is well again.

No comments:

Post a Comment