问题
I need some help implementing a custom role provider in a asp.net mvc app.
The problem is that I'm getting several errors like:
MyRoleProvider does not implement inherited abstract member 'System.Web.Security.RoleProvider.RoleExists(string)
I get the same error for other methods. However, I do have implementations for those...
My web.config has this:
<roleManager enabled="true" defaultProvider="MyCustomProvider">
<providers>
<add name="MyCustomProvider" type="MyRoleProvider" />
</providers>
</roleManager>
My custom role provider is like this (I ommited a few of the methods):
public class MyRoleProvider : RoleProvider {
public override string ApplicationName {
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public override bool RoleExists(string roleName)
{
throw new NotImplementedException();
}
public override bool IsUserInRole(string username, string roleName)
return true;
}
}
What am I doing wrong? (I'm very new to this).
回答1:
When you create a custom provider, especially under Visual studio, Intellisense will fill in the override members' content with:
throw new NotImplementedException();
As you probably know, when inheriting from abstract classes, such as the RoleProvider
class, all abstract members of that class must be implemented. Visual studio chooses when you override a member, to by default fill in the above code. Leaving it as is will allow your project to build since it has been implemented, but at runtime you'll get exceptions because the .net framework will call some of the methods which throw the exception.
What you need to do is remove the throw statement and implement the logic of that method or property. So for IsUserInRole
method, you would check whatever user store you're using (SQL database, XML file, etc.) and return true if the user is in a role, false otherwise.
回答2:
You are throwing a NotImplementedException
in the RoleExists
method. Change it temporarily to return true;
and everything will be fine.
来源:https://stackoverflow.com/questions/1875816/custom-role-provider-does-not-implement-inherited-abstract-member