locked
Seeding users using UserManger and Identify role class thinks its a static class RRS feed

  • Question

  • User-183185495 posted

    I am trying to seed users and permissions but no matter what I do I get the following error.

    But when you look below at my code not one method is a static method so why on earth is visual studio claiming its a static class I dont want to it to be a static one?.

    Severity Code Description Project File Line Suppression State Error CS1106 Extension method must be defined in a non-generic static class Warehouse.Web D:\GitMaster\WareHouseCrm\Warehouse.Web\Helpers\SeedUsers.cs 14 Active

    Line 14 happens to be this line

    public class SeedUsers

    namespace WarehouseCrm.Web.Helpers
    {
        public class SeedUsers
        {
            private WarehouseDBContext _context;
    
            public SeedUsers(WarehouseDBContext context)
            {
                _context = context;
            }
    
            public async void SeedRoles()
            {
                var roleStore = new RoleStore<IdentityRole>(_context);
                await roleStore.CreateAsync(new IdentityRole { Name = "admin", NormalizedName = "admin" });
                await roleStore.CreateAsync(new IdentityRole { Name = "manager", NormalizedName = "manager" });
                await roleStore.CreateAsync(new IdentityRole { Name = "agent", NormalizedName = "agent" });
    
    
            }
            public  async void SeedAdminUser(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager, RoleStore<IdentityRole> roleStore, UserStore<ApplicationUser> userStore)
            {
                var user = new ApplicationUser
                {
                    UserName = "testmanager@outlook.com",
                    NormalizedUserName = "testmanager@outlook.com",
                    Email = "testmanager@outlook.com",
                    NormalizedEmail = "testmanager@outlook.com",
                    FirstName = "test",
                    LastName = "manager",
                    EmailConfirmed = true,
                    LockoutEnabled = false,
                    SecurityStamp = Guid.NewGuid().ToString()
                };         
                
                    var password = new PasswordHasher<ApplicationUser>();
                    var hashed = password.HashPassword(user, "Test12345!");
                    user.PasswordHash = hashed;
                   
                    await userStore.CreateAsync(user);
                    await userStore.AddToRoleAsync(user, "admin");           
            }
    
            public  async void SeedUser1(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager, RoleStore<IdentityRole> roleStore, UserStore<ApplicationUser> userStore)
            {
                var user = new ApplicationUser
                {
                    UserName = "user1@test.com",
                    NormalizedUserName = "user1@test.com",
                    Email = "user1@test.com",
                    NormalizedEmail = "user1@test.com",
                    FirstName = "Martha",
                    LastName = "Jones",
                    EmailConfirmed = true,
                    LockoutEnabled = false,
                    SecurityStamp = Guid.NewGuid().ToString()
                };       
    
     
                    var password = new PasswordHasher<ApplicationUser>();
                    var hashed = password.HashPassword(user, "Test12345!");
                    user.PasswordHash = hashed;
                      await userStore.CreateAsync(user);
                    await userStore.AddToRoleAsync(user, "agent");
                
            }
            public  async void SeedUser2(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager, RoleStore<IdentityRole> roleStore, UserStore<ApplicationUser> userStore)
            {
                var user = new ApplicationUser
                {
                    UserName = "user2@test.com",
                    NormalizedUserName = "user2@test.com",
                    Email = "user2@test.com",
                    NormalizedEmail = "user2@test.com",
                    FirstName = "Matt",
                    LastName = "Smith",
                    EmailConfirmed = true,
                    LockoutEnabled = false,
                    SecurityStamp = Guid.NewGuid().ToString()
                };
             
                    var password = new PasswordHasher<ApplicationUser>();
                    var hashed = password.HashPassword(user, "Test12345!");
                    user.PasswordHash = hashed;
     
                    await userStore.CreateAsync(user);
                    await userStore.AddToRoleAsync(user, "agent");
            }
     
            private  async Task SeedClaimsForSuperAdminAsync(this RoleManager<IdentityRole> roleManager)
            {
                var adminRole =await roleManager.FindByNameAsync("admin");
                await AddPermissionClaim(roleManager,adminRole, "StockItems");
            }
            public List<string> GeneratePermissionsForModule(string module)
            {
                return new List<string>()
                {
                    $"Permissions.{module}.Create",
                    $"Permissions.{module}.View",
                    $"Permissions.{module}.Edit",
                    $"Permissions.{module}.Delete",
                };
            }
            public  async Task AddPermissionClaim( RoleManager<IdentityRole> roleManager, IdentityRole role, string module)
            {
                var allClaims = await roleManager.GetClaimsAsync(role);
                var allPermissions = GeneratePermissionsForModule(module);
                foreach (var permission in allPermissions)
                {
                    if (!allClaims.Any(a => a.Type == "Permission" && a.Value == permission))
                    {
                        await roleManager.AddClaimAsync(role, new Claim("Permission", permission));
                    }
                }
            }
        }
    
    }
    

    Thursday, May 13, 2021 2:06 AM

Answers

  • User-939850651 posted

    Hi roguenidb,

    Following points need to be considered when creating an extension method:

    • The class which defines an extension method must be non-generic, static and non-nested
    • Every extension method must be a static method
    • The first parameter of the extension method should use the this keyword.

    But if you do not intend to have static functions just get rid of the "this" keyword in the arguments.

    In this function(in your code):

    private  async Task SeedClaimsForSuperAdminAsync(this RoleManager<IdentityRole> roleManager)
            {
                var adminRole =await roleManager.FindByNameAsync("admin");
                await AddPermissionClaim(roleManager,adminRole, "StockItems");
            }

    Hope this can help.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 13, 2021 6:39 AM