自定义角色提供者不实现继承的抽象成员 [英] Custom role provider does not implement inherited abstract member
问题描述
我需要实现一个asp.net MVC应用程序自定义角色提供一定的帮助。
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...
我的web.config中有这样:
My web.config has this:
<roleManager enabled="true" defaultProvider="MyCustomProvider">
<providers>
<add name="MyCustomProvider" type="MyRoleProvider" />
</providers>
</roleManager>
我的自定义角色提供程序是这样的(我ommited几个方法):
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).
推荐答案
当你创建一个定制的供应商,特别是在Visual Studio中,智能感知将在覆盖成员的内容与填充:
When you create a custom provider, especially under Visual studio, Intellisense will fill in the override members' content with:
throw new NotImplementedException();
正如你可能知道,从抽象类,如 RoleProvider
类继承时,该类的所有抽象成员必须执行。当你在上面code重写一员,在默认情况下填充Visual Studio中进行选择。把它当作是可以让你的项目建设,因为它已经实施,但在运行时你会得到的例外,因为.NET框架将调用一些它抛出异常的方法。
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.
您需要做的是去除throw语句和实施方法或属性的逻辑。因此,对于的isUserInRole
方法,你会什么的用户存储你使用(SQL数据库,XML文件等),如果用户是在一个角色返回true检查,假的否则。
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.
这篇关于自定义角色提供者不实现继承的抽象成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!