Custom role provider does not implement inherited abstract member

六眼飞鱼酱① 提交于 2019-12-10 19:09:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!