自定义角色提供程序不实现inheritance的抽象成员

我需要一些帮助在asp.net mvc应用程序中实现自定义角色提供程序。

问题是我遇到了几个错误:

MyRoleProvider does not implement inherited abstract member 'System.Web.Security.RoleProvider.RoleExists(string) 

我得到其他方法相同的错误。 但是,我确实有那些……

我的web.config有这个:

      

我的自定义角色提供程序是这样的(我省略了一些方法):

 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; } } 

我究竟做错了什么? (我对此很新)。

当您创建自定义提供程序时,尤其是在Visual Studio下,Intellisense将使用以下内容填写覆盖成员的内容:

 throw new NotImplementedException(); 

您可能知道,在inheritance自抽象类(如RoleProvider类)时,必须实现该类的所有抽象成员。 Visual Studio在覆盖成员时选择,默认情况下填写上面的代码。 保持原样可以让你的项目在实现之后进行构建,但是在运行时你会得到exception,因为.net框架会调用一些抛出exception的方法。

您需要做的是删除throw语句并实现该方法或属性的逻辑。 因此,对于IsUserInRole方法,您将检查您正在使用的任何用户存储(SQL数据库,XML文件等),如果用户处于角色中则返回true,否则返回false。

您在RoleExists方法中NotImplementedException 。 暂时改变它以return true; 一切都会好的。