如何让用户不在角色中,

发布于 2024-10-22 03:16:28 字数 132 浏览 1 评论 0原文

是否有任何相当有效的方法来获取不属于特定角色的用户列表?

我能看到的唯一方法是

  1. 从数据库获取所有用户并执行代码签入

  2. 直接进入数据库并回避角色提供者

Is there any reasonably efficient way to get a list of users who are not in a specific role?

The only methods I can see are

  1. Get all the users from DB and do a check in code

  2. Go directly to the db and sidestep the role provider

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

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

发布评论

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

评论(5

清风夜微凉 2024-10-29 03:16:28

您可以获取所有用户列表并提取列表中指定角色的用户:

var usersInRole = Roles.GetUsersInRole("admin");
var users = Membership.GetAllUsers()
    .Cast<MembershipUser>()
    .Select(u => 
        !usersInRole.Contains(u.UserName)
    );

You could just get the all user list and extract users in role specified from the list:

var usersInRole = Roles.GetUsersInRole("admin");
var users = Membership.GetAllUsers()
    .Cast<MembershipUser>()
    .Select(u => 
        !usersInRole.Contains(u.UserName)
    );
衣神在巴黎 2024-10-29 03:16:28

在 Alex 提供的代码中将 Select 更改为Where 将实际返回结果,而不仅仅是 true/false。

var usersInRole = Roles.GetUsersInRole("admin");
var users = Membership.GetAllUsers()
    .Cast<MembershipUser>()
    .Where(u => 
        !usersInRole.Contains(u.UserName)
    );

Changing the Select to Where in the code provided by Alex will actually return the results instead of just true/false.

var usersInRole = Roles.GetUsersInRole("admin");
var users = Membership.GetAllUsers()
    .Cast<MembershipUser>()
    .Where(u => 
        !usersInRole.Contains(u.UserName)
    );
雨后咖啡店 2024-10-29 03:16:28

另一种方法是使用 GetUsersNotInRole() 方法扩展 RoleProvider 并在那里查询数据库。您还可以将 RoleProvider.GetUsersInRole() 与 MembershipProvider.GetAllUsers() 结合起来并找出区别

Another way is to extend RoleProvider with the method GetUsersNotInRole() and query the DB there. You can also combine RoleProvider.GetUsersInRole() with MembershipProvider.GetAllUsers() and find the difference

嘿哥们儿 2024-10-29 03:16:28

我不认为绕过角色提供者直接查询数据库有什么问题。这样你一定会得到最好的表现。

I don't think that there is anything wrong with by-passing the role provider and querying the database directly. You will definitely get the best performance this way.

爱殇璃 2024-10-29 03:16:28

找到这个方法,希望这对其他人也有帮助。很容易。

 var usernames = Roles.GetUsersInRole("Administrator");

            var adminUsers = db.UserProfiles
                 .Where(x => !usernames.Contains(x.UserName)).ToList();
            return View(adminUsers);

Found this way, hope this helps others aswell. Very easy.

 var usernames = Roles.GetUsersInRole("Administrator");

            var adminUsers = db.UserProfiles
                 .Where(x => !usernames.Contains(x.UserName)).ToList();
            return View(adminUsers);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文