Example #1
0
        protected static async void AddUser(IPCSOAuthContext context, string username, string password, bool isAdmin, UserManager <User> userManager)
        {
            var verifyUser = await context.Users.Where(w => w.UserName == username).FirstOrDefaultAsync();

            if (verifyUser == null)
            {
                verifyUser = new User
                {
                    UserName       = username,
                    FirstName      = username,
                    LastName       = username,
                    Email          = username,
                    CreationDate   = DateTime.Now,
                    EmailConfirmed = true,
                    Valid          = true,
                    PhoneNumber    = "55555555555555"
                };
                var result = await userManager.CreateAsync(verifyUser, password);
            }
            var isInRole = await userManager.IsInRoleAsync(verifyUser, "ROLE_USER");

            if (!isInRole)
            {
                await userManager.AddToRoleAsync(verifyUser, "ROLE_USER");
            }
            isInRole = await userManager.IsInRoleAsync(verifyUser, "ROLE_ADMIN");

            if (!isInRole)
            {
                await userManager.AddToRoleAsync(verifyUser, "ROLE_ADMIN");
            }

            await context.SaveChangesAsync();
        }
Example #2
0
        protected static async void AddRole(IPCSOAuthContext context, string roleName)
        {
            if (context.Roles.Where(w => w.Name == roleName).Count() == 0)
            {
                await context.Roles.AddAsync(new IdentityRole { Name = roleName, NormalizedName = roleName });

                await context.SaveChangesAsync();
            }
        }