用户角色和授权

发布于 2024-11-19 00:12:36 字数 330 浏览 14 评论 0原文

因此,我想创建一个登录页面,当您以管理员身份输入登录凭据时,您可以获得访问权限。如果您不是管理员,您将被重定向回登录页面。在我的数据库中,我有一个布尔类型的字段:

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 技术交流群。

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

发布评论

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

评论(3

‖放下 2024-11-26 00:12:36

如果我理解正确的话,我也遇到过类似的问题。从您的问题看来,您没有使用默认的会员资格提供商(至少是这样)。我也没有。所以我所做的是创建一个新的授权属性。在您的情况下,它可能看起来像这样:

public class AdminOnlyAttribute : AuthorizeAttribute {
    IUserRepository _UserRepository;

    public SimpleUser SimpleUser { get; set; }

    public AdminOnlyAttribute() {
        _UserRepository = new SqlUserRepository(new DbContext());
    }

    protected override bool AuthorizeCore(HttpContextBase httpContext) {
        bool baseAuthorized = base.AuthorizeCore(httpContext);
        if (!baseAuthorized) {
            return false;
        } 

        //Here you use your repository to check if a user is an admin or not
        bool isAdmin = _UserRepository.IsAdmin(int.Parse(httpContext.User.Identity.Name));

        if (!isAdmin) {
            return false;
        }

        return true;
    }
}

存储库方法 IsAdmin 可以像查询一样简单,用于检查与所提供的用户 ID 相对应的布尔值。像这样的东西(请仔细检查 SingleOrDefault() 是否必要):

public bool IsAdmin(int userID) {
    bool isAdmin = (from user in db.Users
                    where user.ID == userID
                    select user.isAdmin).SingleOrDefault();
    return isAdmin;
}

然后在您想要的操作中使用它,如下所示:

[AdminOnly]
public ActionResult Index(){
    //Code here...
}

当返回 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:

public class AdminOnlyAttribute : AuthorizeAttribute {
    IUserRepository _UserRepository;

    public SimpleUser SimpleUser { get; set; }

    public AdminOnlyAttribute() {
        _UserRepository = new SqlUserRepository(new DbContext());
    }

    protected override bool AuthorizeCore(HttpContextBase httpContext) {
        bool baseAuthorized = base.AuthorizeCore(httpContext);
        if (!baseAuthorized) {
            return false;
        } 

        //Here you use your repository to check if a user is an admin or not
        bool isAdmin = _UserRepository.IsAdmin(int.Parse(httpContext.User.Identity.Name));

        if (!isAdmin) {
            return false;
        }

        return true;
    }
}

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):

public bool IsAdmin(int userID) {
    bool isAdmin = (from user in db.Users
                    where user.ID == userID
                    select user.isAdmin).SingleOrDefault();
    return isAdmin;
}

And then use this in the action you want like so:

[AdminOnly]
public ActionResult Index(){
    //Code here...
}

When this returns false, your ActionResult will be an HttpUnauthorizedResult which in theory should redirect to the login page.

无妨# 2024-11-26 00:12:36

您应该创建一个自定义成员资格提供程序并检查用户 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/

迷雾森÷林ヴ 2024-11-26 00:12:36

你的 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.

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