更好的方法让用户不属于任何角色?

发布于 2024-10-27 20:50:22 字数 437 浏览 10 评论 0原文

我编写的以下内容确实有效,但想知道是否有更有效的方法来获取所有没有任何角色的用户。

using System.Collections.Generic;
using System.Linq;
using System.Web.Security;

public static IEnumerable<MembershipUser> GetUsersHavingNoRole() {
  var allUsers = Membership.GetAllUsers().Cast<MembershipUser>();
  foreach (var user in allUsers) {
    if (Roles.GetRolesForUser(user.UserName).Length.Equals(0)) {
      yield return user;
    }
  }
}

I wrote the following that does work, but want to know if there is any more efficient ways to get all users without any role.

using System.Collections.Generic;
using System.Linq;
using System.Web.Security;

public static IEnumerable<MembershipUser> GetUsersHavingNoRole() {
  var allUsers = Membership.GetAllUsers().Cast<MembershipUser>();
  foreach (var user in allUsers) {
    if (Roles.GetRolesForUser(user.UserName).Length.Equals(0)) {
      yield return user;
    }
  }
}

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

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

发布评论

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

评论(3

好菇凉咱不稀罕他 2024-11-03 20:50:23

我预计用户通常比角色多 - 因此迭代角色可能是有意义的(由 Roles.GetAllRoles() 返回),建立一个任何这些角色中的用户列表(例如,通过创建 HashSet 并为 Roles.GetUsersInRole 返回的每个角色添加用户),然后查找差异在该用户和所有用户之间。因此,如果您使用 LINQ,则可以使用:

var usersInRolesQuery = Roles.GetAllRoles()
                             .SelectMany(role => Roles.GetUsersInRole(role));

var usersInRoles = new HashSet<string>(usersInRolesQuery);
return Membership.GetAllUsers()
                 .Cast<MembershipUser>()
                 .Where(user => !usersInRoles.Contains(user.UserName));

当然,这仍然处理相同数量的数据 - 但它可能意味着到所涉及的任何数据存储的往返次数更少。

您是否对应用程序进行了基准测试以了解当前方法的成本如何?

I'd expect there to usually be more users than roles - so it might make sense to iterate over the roles (return by Roles.GetAllRoles()), build up a list of users in any of those roles (e.g. by creating a HashSet<string> and adding the users for each role returned by Roles.GetUsersInRole), and then finding the difference between that and all the users. So if you're using LINQ, you could use:

var usersInRolesQuery = Roles.GetAllRoles()
                             .SelectMany(role => Roles.GetUsersInRole(role));

var usersInRoles = new HashSet<string>(usersInRolesQuery);
return Membership.GetAllUsers()
                 .Cast<MembershipUser>()
                 .Where(user => !usersInRoles.Contains(user.UserName));

Of course that's still dealing with the same amount of data - but it may mean fewer roundtrips to whatever datastore is involved.

Have you benchmarked the application to find out how expensive your current method is?

你不是我要的菜∠ 2024-11-03 20:50:23

根据用户、角色和它们之间的关系的数量以及您需要此信息的次数,可能值得(有足够的免责声明了吗?)向 (自定义)角色提供者。

提供程序中所需的本机查询可能比您现在使用 LINQ 的方式高效得多。

很大程度上取决于这个答案第一行的因素,也取决于提供商。可维护性也可能是一个问题,因为您添加的查询将来可能不受支持(提供程序的下一版本)

Depending on the amount of users, roles and relations in between them and the number of times you need this information, it might be worth it (had enough disclaimers yet?) to add an extra method to the (custom)Role Provider.

The native query that is required in the provider could be much more efficient than using LINQ the way you are doing now.

A lot depends on the factors in first line of this answer but also on the provider. Maintainability might also be an issue as you are adding a query that might not be supported in the future (next version of the provider)

酒绊 2024-11-03 20:50:23

如果我正确理解你的问题。这段代码应该有帮助!

MembershipUserCollection users = Membership.GetAllUsers();
MembershipUserCollection usersNoRoles = new MembershipUserCollection();

    foreach (MembershipUser user in users)
    {
        string[] roles = Roles.GetRolesForUser(user.UserName);

        // if roles empty
        if (roles.Count() == 0)
        {
            // Add User to a List for User with no Roles
            usersNoRoles.Add(user);
        }

   }

If I understand your question properly. This code should help!

MembershipUserCollection users = Membership.GetAllUsers();
MembershipUserCollection usersNoRoles = new MembershipUserCollection();

    foreach (MembershipUser user in users)
    {
        string[] roles = Roles.GetRolesForUser(user.UserName);

        // if roles empty
        if (roles.Count() == 0)
        {
            // Add User to a List for User with no Roles
            usersNoRoles.Add(user);
        }

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