Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

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

$
0
0

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 StateError 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));
                }
            }
        }
    }

}


Viewing all articles
Browse latest Browse all 1698

Trending Articles



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