没有数据库的 ASP.NET MVC 角色(并且没有角色提供程序)
我有一个超级简单的 ASP.NET MVC 应用程序,它使用 RpxNow (OpenID) 来允许用户登录。我现在想让用户编辑自己的帐户,并提供管理员访问权限来编辑任何人的帐户。
我有两个单独的“编辑帐户”视图:
- ~/account/edit/
- ~/account/edit/1
第一个根据登录用户加载帐户详细信息。第二个使用提供的 AccountId 加载帐户详细信息。第一个适用于标准用户,第二个适用于管理员。
首先,我需要定义角色(用户、管理员),然后需要为该角色分配一个(或多个)用户帐户。
然后我需要检查控制器中的角色。我喜欢这个概念:
http: //schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/
那么,回到问题:
- 是否有一种简单的方法来定义角色列表在 web.config 中?
- 有没有一种简单的方法来定义哪些用户在 web.config 中担任哪些角色?
- 有没有办法在不使用成员/角色提供者的情况下做到这一点?
- 我是否从错误的角度看待这个问题?我是否应该将应用程序分为两个分支并根据文件夹授权来保护它们?
I have a super simple ASP.NET MVC application that uses RpxNow (OpenID) to allow users to login. I now want to let users edit their own account and provide administrator access to edit anyone's account.
I have two separate "Edit Account" views:
- ~/account/edit/
- ~/account/edit/1
The first loads the account details based on the logged in user. The second loads the account details using the supplied AccountId. The first would be for standard users, and the second for an administrator.
Firstly I need to define the roles (User, Admin) and then I need to assign a user account (or multiple) to that role.
Then I need to check the role in the controller. I like this concept:
http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/
So, down to the questions:
- Is there a simple way to define a list of roles in the web.config?
- Is there a simple way to define which users are in which roles in the web.config?
- Is there a way to do this WITHOUT using Membership / Role providers?
- Am I approaching this from the wrong perspective? Should I be partioning the application into two branches and securing them based on folder authorisation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不喜欢在 web.config 中存储授权数据。我更喜欢将其存储在数据库或其他 xml 文件中。
看看 Xml 成员资格/角色提供者。它使用成员身份/角色来读取用户数据,但它显示了一种从 xml 文件存储和读取用户授权数据的方法。
分支应用程序只会转移问题而不是解决问题。
I'm not a friend of storing authorization data in web.config. I prefer storing it in database or other xml files.
Have a look at Xml Membership / Role Provider. This uses Membership / Role for reading userdata but it shows a way storing and reading user authorization data from xml files.
Braching the application woulded move the issue and not solve.
请记住,整个权限管道实际上仍然围绕 IPrincipals,角色/成员身份提供程序只是装饰门面,以允许大多数应用程序不必编写该管道代码。在这种情况下,您可以轻松添加数据库支持的(或者如果列表足够短,则只是静态的)角色列表和角色中的用户列表并进行查询。把它包裹在一个定制的 IPrincipal 后面,然后把小狗塞到适当的地方,你就万事大吉了。
Remember that the entire permissions plumbing still really revolves around IPrincipals, the Role/Membership providers are just window dressing to allow most applications to not have to write that plumbing code. In this case, you could easily add a database-backed (or just static if the list is short enough) list of roles and a list of users in roles and query that. Wrap it up behind a custom IPrincipal and stuff that puppy in there at the appropriate place and you are golden.