如何将多个值传递给 MVC 应用程序中的自定义 RoleProvider GetUserRoles 方法?

发布于 2024-08-20 15:29:32 字数 992 浏览 8 评论 0原文

我想将我的自定义权限系统粘合到新的 ASP.NET MVC 应用程序中。我已经做了相当多的工作,为应用程序编写自己的自定义 AuthorizeAttribute。我还编写了自己的自定义 MembershipProvider 和 RoleProvider 实现。所有这些工作都很顺利,直到我需要检查我的用户所属的角色。 我做了这一切,所以我可以简单地在我的控制器上执行此操作,并且我使用 VS2010 中的默认 MVC 模板来改造我的代码作为原型:

[CustomAuth(Roles='X')]
public ActionResult DoSomething()

不过,我的自定义系统有点古怪。我不调用 SQL 数据库。它是一个简单的 Web 服务,提供 3 个方法:CheckPassword、GetPermissions、WebLogin。 WebLogin 只是将 CheckPassword 和 GetPermissions 包装到一个调用中。当从 ValidateUser 中使用 WebLogin 不起作用时,我从 MembershipProvider.ValidateUser(userName,password) 中调用 TestPassword,当此调用成功时,我将返回 UserID,否则它将失败并且我将返回 NULL。

问题是当我调用 RoleProvider 的实现时。抽象类使用单个参数 userName 定义了 GetRolesForUser。我的 GetPermissions() 方法需要多个用户名才能工作。它的定义如下:

GetPermissions(privateKey,userName)

显然,从以下 RoleProvider 方法中调用它是行不通的。我无法将 privateKey 传递给开箱即用支持的方法。

public override string[] GetRolesForUser(string username)

是否不可能从我自己的自定义 RoleProvider 中调用我自己的自定义方法?

I'd like to glue my custom permissioning system into a new ASP.NET MVC app. I've done a fair amount of work writing my own custom AuthorizeAttribute for the app. I've also written my own custom MembershipProvider and RoleProvider implementations. All of this works beautifully, up until I need to check the Roles my user is a part of.
I did all this so I could simply do this on my controllers, and I used the default MVC template in VS2010 to retro fit my code as a prototype:

[CustomAuth(Roles='X')]
public ActionResult DoSomething()

My custom system is a bit whacky though. I don't call into a SQL db. It is a simple webservice that provides 3 methods: CheckPassword, GetPermissions, WebLogin. WebLogin simply wraps both CheckPassword and GetPermissions into one call. When using WebLogin from within ValidateUser didn't work I called TestPassword from within MembershipProvider.ValidateUser(userName,password) and when this call was successfull I would get back the UserID, else it would fail and I'd get back a NULL.

The problem is when I call into my implementation of RoleProvider. The abstract class defined GetRolesForUser with a single parameter of userName. My GetPermissions() method needs more than a single userName to work. It is defined like so:

GetPermissions(privateKey,userName)

Clearly, calling this from within the following RoleProvider method isn't going to work. I can't pass privateKey to the method that is supported out of the box.

public override string[] GetRolesForUser(string username)

Is it impossible to get my own custom method called from within my own custom RoleProvider?

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

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

发布评论

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

评论(2

肥爪爪 2024-08-27 15:29:32

您可以将角色提供者转换为您的自定义类型,这将使​​您能够访问您的方法。因为您的函数位于接口之外,所以这将您的应用程序与您的角色提供者紧密耦合 - 这可能适合您的目的。

    RoleProvider rp = Roles.Provider;
    (rp as MyRoleProvider).GetPermissions(privateKey,userName);

You can cast the role provider as your custom type, which will give you access to your methods. Because your function is outside the interface though, this tightly couples your application to your role provider - which is probably fine for your purposes.

    RoleProvider rp = Roles.Provider;
    (rp as MyRoleProvider).GetPermissions(privateKey,userName);
谈场末日恋爱 2024-08-27 15:29:32

我认为应该是:

(RoleProvider as MyRoleProvider).GetPermissions(privateKey,userName);

I think it should be:

(RoleProvider as MyRoleProvider).GetPermissions(privateKey,userName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文