覆盖角色提供程序 - ASP.NET

发布于 2024-08-08 02:05:03 字数 1135 浏览 5 评论 0原文

编辑:通过重写 RoleProvider,我们希望仍然在新方法中使用相同的方法。例如 Roles.GetRolesForUser(int)。

 string[] myRoles3 = ((PortalRoleProvider) (Roles.Provider)).GetRolesForUser(2);

上面的代码看起来有点多余?

我有一个类,它重写 RoleProvider 的一些成员并添加新方法。例如。

 public string[] GetRolesForUser(int UserId)
    {
        IEnumerable<Role> RoleList = _Repo.GetRolesForUser(UserId);


        string[] RetVal = new string[] {};

        foreach (var curRole in RoleList)
        {
            RetVal[RetVal.GetUpperBound(0)] = curRole.RoleName;
        }
        return RetVal;
    }

现在,当我在代码逻辑中时,我想编写 Roles.GetRolesForUser(2) 但当我键入此内容时,它似乎确实看到了我添加的额外方法。这是类声明和 webconfig。

 namespace PortalMVC.Providers
 {
    public class PortalRoleProvider : RoleProvider
    {

Web.config

<roleManager enabled="true" defaultProvider="PortalRoleProvider">
    <providers>
        <clear/>
         <add name="PortalRoleProvider" type="PortalMVC.Providers.PortalRoleProvider"/>
    </providers>
</roleManager>

有什么建议吗?

Edit: By Overriding the RoleProvider we were hoping to still use the same methods with our new methods. Such as Roles.GetRolesForUser(int).

 string[] myRoles3 = ((PortalRoleProvider) (Roles.Provider)).GetRolesForUser(2);

That above code seems a bit excessive ?

I have a class which overrides some members of the RoleProvider and adds new methods. Eg.

 public string[] GetRolesForUser(int UserId)
    {
        IEnumerable<Role> RoleList = _Repo.GetRolesForUser(UserId);


        string[] RetVal = new string[] {};

        foreach (var curRole in RoleList)
        {
            RetVal[RetVal.GetUpperBound(0)] = curRole.RoleName;
        }
        return RetVal;
    }

Now when I am in the logic of my code I want to write Roles.GetRolesForUser(2) but when i type this, it does seem to see the extra method that I have added. Here is the class declaration and the webconfig.

 namespace PortalMVC.Providers
 {
    public class PortalRoleProvider : RoleProvider
    {

Web.config

<roleManager enabled="true" defaultProvider="PortalRoleProvider">
    <providers>
        <clear/>
         <add name="PortalRoleProvider" type="PortalMVC.Providers.PortalRoleProvider"/>
    </providers>
</roleManager>

Any suggestions why this is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筑梦 2024-08-15 02:05:03

您必须将 RoleProvider 转换为实际对象类型(即 PortalRoleProvider)才能看到额外的功能。这适用于任何时候您扩展了标准对象,但将其作为超类(母版页等)进行访问。

string[] myRoles3 = ((PortalRoleProvider) (Roles.Provider)).GetRolesForUser(2) 。

正如您在帖子中指出的那样,上述内容是有效的 如果您需要在一段代码中大量使用它,您可以创建一个具有正确类型的指针,然后从那里访问方法:


PortableRoleProvider TempPointer=(PortableRoleProvider)Roles.Provider;

string[] myRoles3=TempPointer.GetRolesForUser(2);

You have to cast your RoleProvider as your actual object type (i.e. PortalRoleProvider) to see the extra functions. This applies to any time you've extended a standard object but are accessing it as it's super class (Master pages, etc.)

string[] myRoles3 = ((PortalRoleProvider) (Roles.Provider)).GetRolesForUser(2);

The above, as you've pointed out in your post, works. If you need to use this a lot in a section of code, you can create a pointer with the proper type then access the methods from there:


PortableRoleProvider TempPointer=(PortableRoleProvider)Roles.Provider;

string[] myRoles3=TempPointer.GetRolesForUser(2);

好多鱼好多余 2024-08-15 02:05:03

在 web.config 中指定角色提供程序的类型时,您可能需要添加可以找到该类型的程序集的名称。像这样的东西:

<add name="PortalRoleProvider" type="PortalMVC.Providers.PortalRoleProvider, MyAssemblyName" />

In the web.config, where you specify the type for the role provider, you might need to add the name of the assembly where the type can be found. Something like this:

<add name="PortalRoleProvider" type="PortalMVC.Providers.PortalRoleProvider, MyAssemblyName" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文