1

I'm writing a role provider to supply user role information to an ASP.NET MVC 4 framework I'm using a library called JDash. The JDash documentation calls for me to write a provider implementing System.Web.Security.RoleProvider, then set its type name in a JDash .config file. So I wrote a stub of my role provider and gave JDash its type name, and JDash found the setting properly and tried to instantiate my RoleProvider. However, it kept failing with:

"Custom role provider must implement System.Web.Security.RoleProvider abstract class"

Even though I was absolutely sure that my class was implementing RoleProvider properly.

So I took a look at the JDash source code and found how it's checking that the set role provider implements RoleProvider:

Type type = Type.GetType(config["roleProvider"]);
if (!(type == typeof (RoleProvider)))
      throw new ArgumentException("Custom role provider must implement System.Web.Security.RoleProvider abstract class");

The problem is, GetType() returns the actual runtime type of my role provider...which is not System.Web.Security.RoleProvider, it's a class derived from System.Web.Security.RoleProvider, so of course it doesn't match typeof(RoleProvider). My class and RoleProvider are of different types; one just happens to be derived from the other, a fact that typeof happily ignores. So if I'm looking at this right, JDash has a bug that makes it impossible to give it a role provider that returns its runtime type as something other than RoleProvider.

I'll be reporting this bug and hoping for a fix, but for the time being, is there some way I can work around it? Could I somehow make GetType("MyRoleProviderDerivedClass") return the type of RoleProvider? Or can I trick typeof somehow, so that it thinks MyRoleProviderDerivedClass is the same as the RoleProvider base class?

d219
  • 2,707
  • 5
  • 31
  • 36
  • Given that the fix is really simple (use `Type.IsAssignableFrom`) you could probably just fork while you're waiting for the fix... – Jon Skeet Jul 17 '15 at 14:12
  • @JonSkeet I don't properly have access to the JDash source code - I found that type checking bit by decompiling. – The Insoluble Lurnip Jul 17 '15 at 14:18
  • Urgh - had assumed this was open source. Not a lot you can do then, I suspect. That looks completely broken by design, to be honest. – Jon Skeet Jul 17 '15 at 14:19
  • Apologies for closing as a dupe, wasn't paying attention. That said, not really answerable other than to submit a bug report to the original developers. – Adam Houldsworth Jul 17 '15 at 14:22
  • Does `config["roleProvider"]` reference a configuration file, as the name suggests, perhaps as a kludgy way of dealing with precisely this issue (rather than just `IsAssignableFrom` with `RoleProvider`)? – Jon Hanna Oct 10 '18 at 15:00

1 Answers1

0

There is no way around this because you can't hook into the comparison or into typeof. This is a simple reference comparison.

usr
  • 168,620
  • 35
  • 240
  • 369
  • Unfortunately, it looks like this is how it is. On a whim I tried writing a new MyRoleProvider.GetType() method to hide Object.GetType(), but that didn't help. – The Insoluble Lurnip Jul 17 '15 at 14:27
  • GetType is not virtual, nobody will call that new method. We don't want objects to be able to lie about their type. – usr Jul 17 '15 at 14:40