除了使用 RoleProvider 之外还有其他选择吗?
我正在开发一个应用程序,其中用户可以属于两个用户“组”(教师和学生)。根据我的需求,用户永远不需要从一个角色转移到另一个角色。虽然我当然可以创建一个自定义 RoleProvider
,但对于我的需要来说,它似乎有点过分了。
到目前为止,我一直在使用由 Visual Studio 生成的 MembershipService
的修改版本:我创建了函数 IsTeacher(IPrincipal user)
,该函数依次查询我的MembershipProvider
。这个解决方案在控制器中工作得很好,但在视图方面却不够好(我可以做到,只是感觉笨拙/闻起来很糟糕)。
我如何最好地创建一个类,让我从视图中确定用户是否是教师?或者,我是否想得太多了,编写一个 RoleProvider 就可以了?
I'm working on an application where a user can be in one two 'groups' of users (teachers and students). For my needs, a user will never need to be moved from one role to another. While I certainly could create a custom RoleProvider
, it seems a bit overblown for what I need.
Up to this point, I've been using a modified version of the MembershipService
that is generated by Visual Studio: I created the function IsTeacher(IPrincipal user)
which in turn queries my MembershipProvider
. This solution works well enough in controllers, but it falls short in views (I can do it, it just feels clumsy/smells bad).
How would I best go about creating a class that would let me determine if a user is a teacher from a view? Alternatively, am I over-thinking this and would be fine writing a RoleProvider
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的观点是强类型的吗?似乎您可以使用 ViewModel 将适当的用户相关信息作为布尔属性从控制器传递到视图。
ViewModel 模式示例 1
ViewModel 模式示例 2
Are your views strongly-typed? Seems you could use a ViewModel to pass appropriate user-related information from the controller to the view as boolean properties.
ViewModel Pattern Example 1
ViewModel Pattern Example 2