Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Get User in Registered Role

$
0
0

I have custom User, Role and UserRole table, with User having an ICollection of UserRole

    public partial class UserRoleMap
    {
        public UserRoleMap(EntityTypeBuilder<UserRole> builder)
        {
            builder.ToTable("UserRole");
            builder.HasKey(ur => new { ur.UserId, ur.RoleId });

            builder.HasOne(ur => ur.User)
                .WithMany(u => u.UserRoles)
                .HasForeignKey(ur => ur.UserId);
        }
    }

Here is code snippet from UserExtensions.cs class

        public static bool IsInRole(this User user, string roleSystemName,
            bool onlyActiveRoles = true)
        {
            if (user == null)
                throw new ArgumentNullException(nameof(user));
            if (string.IsNullOrEmpty(roleSystemName))
                throw new ArgumentNullException(nameof(roleSystemName));
            return user.UserRoles.FirstOrDefault(ur => (!onlyActiveRoles
                || ur.Role.Active) && (ur.Role.SystemName == roleSystemName)) != null;
        }

        public static bool IsRegistered(this User user, bool onlyActiveRoles = true)
        {
            return IsInRole(user, SystemRoleNames.Registered, onlyActiveRoles);
        }

Now whenever I try to use this in project

if (!user.IsRegistered())
    ... something to happen ...

It doesn't seem to work. Any info would be greatly appreciated. Thank you!


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>