用户角色和授权
因此,我想创建一个登录页面,当您以管理员身份输入登录凭据时,您可以获得访问权限。如果您不是管理员,您将被重定向回登录页面。在我的数据库中,我有一个布尔类型的字段:
isAdmin <--datatype(byte")
那么如何才能最好地做到这一点?!我想以存储库模式的方式执行此操作,因为这样可以更轻松地对其进行单元测试。
我在谷歌上搜索了很多,并开始对此事感到有点困惑。我应该有多少个类、模型等?我猜一个控制器就可以了。大家有什么好主意吗?!我读过一些关于用户角色的 DCI 模式,但因为它基本上“仅”检查数据库中的布尔值,也许它是多余的?感谢所有反馈。
So I want to create a login page where when you enter your login credentials as a admin you get acces. If you are not a admin you get redirected back to the login page. In my database I have a field of boolean type:
isAdmin <--datatype(byte")
So how can you the best way do this?! I would like to do this in the repository pattern way as it gets easier to unit test it then.
I have googled this a lot and starting to get a bit confused on the matter. How many classes, models etc should I have?! I'm guessing one controller would do. Anyone got any good ideas?! I've read some on the DCI pattern about user roles but as it basically "only" to check that boolean in the database maybe it is overkill? Thankful for all feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解正确的话,我也遇到过类似的问题。从您的问题看来,您没有使用默认的会员资格提供商(至少是这样)。我也没有。所以我所做的是创建一个新的授权属性。在您的情况下,它可能看起来像这样:
存储库方法 IsAdmin 可以像查询一样简单,用于检查与所提供的用户 ID 相对应的布尔值。像这样的东西(请仔细检查
SingleOrDefault()
是否必要):然后在您想要的操作中使用它,如下所示:
当返回 false 时,您的 ActionResult 将是一个 HttpUnauthorizedResult 理论上应该重定向到登录页面。
If I understand correctly, I had a similar issue. It seems from your question that you are not using the default membership provider (at least as is). I didn't either. So what I did was create a new authorization attribute. In your case it could look something like this:
The repository method IsAdmin could be as simple as a query to check the boolean corresponding to the supplied user's ID. Something like this (please double check if
SingleOrDefault()
is necessary or not):And then use this in the action you want like so:
When this returns false, your ActionResult will be an HttpUnauthorizedResult which in theory should redirect to the login page.
您应该创建一个自定义成员资格提供程序并检查用户 isAdmin 作为 ValidateUser 的一部分。
或者,如果允许其他用户进入,请使用自定义角色提供程序。
以下链接是一个很好的起点
http://theintegrity.co.uk/2010/11/asp-net-mvc-2-custom-membership-provider-tutorial-part-1/
You should create a custom Membership Provider and check the user isAdmin as part of ValidateUser.
Alternatively if other users are allowed in, use a custom role provider.
The following link is a good place to start
http://theintegrity.co.uk/2010/11/asp-net-mvc-2-custom-membership-provider-tutorial-part-1/
你的 isAdmin 列是一个位还是一个字节?应该是有一点吧。您只需创建一个查询来检查凭据和 IsAdmin 列。如果返回一行则登录成功。
Is your isAdmin column a bit or a byte? It should probably be a bit. You could just create a query that checks the credentials and the IsAdmin column. If a row is returned then the login was successful.