Example #1
0
        private static void seedUserRoles(string Admin, string User, ApplicationDbContext db)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

            manager.AddToRoles(Admin, Roles.ADMIN);
            manager.AddToRole(User, Roles.USER);
        }
        public void Seed(ApplicationDbContext context)
        {
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

            if (!context.Users.Any(u => u.UserName == "IvkoBivko"))
            {
                var user = new ApplicationUser
                {
                    Email = "[email protected]",
                    UserName = "IvkoBivko",
                    CreatedOn = DateTime.Now
                };

                userManager.Create(user, "123456");

                userManager.AddToRoles(
                    user.Id,
                    ApplicationRoles.Admin,
                    ApplicationRoles.Artist,
                    ApplicationRoles.Designer,
                    ApplicationRoles.Regular,
                    ApplicationRoles.Seller,
                    ApplicationRoles.Student,
                    ApplicationRoles.Trainer);
            }
        }
Example #3
0
        /// <summary>
        /// Adds user to a AspNetRole and all other roles that are lower in the Roles Enum hierarchy
        /// </summary>
        /// <param name="user">Application User</param>
        /// <param name="role">Roles enum role</param>
        public static void AddUserToRole(ApplicationUser user, Roles role)
        {
            var context = new ApplicationDbContext();
            var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

            List<string> roles = new List<string>();

            if (roleManager.RoleExists(role.ToString()))
            {
                //ascertain level, add to this role and every role beneath it
                int level = (int)role;
                foreach (Roles roleVal in Enum.GetValues(typeof(Roles)))
                {
                    if ((int)roleVal >= level)
                    {
                        roles.Add(roleVal.ToString());
                    }
                }
                UserManager.AddToRoles(user.Id, roles.ToArray());
            }
            else
            {
                throw new Exception("Role does not exist");
            }
        }
        public ActionResult Create([Bind(Include = "FirstName,LastName,Email,PhoneNumber,UserName")] ApplicationUser applicationUser)
        {
            if (ModelState.IsValid)
            {
                var userStore = new UserStore<ApplicationUser>(db);
                var userManager = new UserManager<ApplicationUser>(userStore);

                var user = userManager.FindByName(applicationUser.UserName);
                if (user == null)
                {
                    userManager.Create(applicationUser);
                    userManager.SetLockoutEnabled(applicationUser.Id, false);
                    userManager.AddToRoles(applicationUser.Id, Request["Role"]);

                    return RedirectToAction("Index");
                }
            }

            return View(applicationUser);
        }
        public ActionResult Edit([Bind(Include = "FirstName,LastName,Email,PhoneNumber,UserName")] ApplicationUser applicationUser)
        {
            if (ModelState.IsValid)
            {
                var userStore = new UserStore<ApplicationUser>(db);
                var userManager = new UserManager<ApplicationUser>(userStore);

                string userId = userManager.FindByName(applicationUser.UserName).Id;

                userManager.RemoveFromRoles(userId, userManager.GetRoles(userId).ToArray());
                userManager.AddToRoles(userId, Request["Role"].Split(','));

                userManager.Update(applicationUser);

                return RedirectToAction("Index");
            }
            return View(applicationUser);
        }
Example #6
0
 private static void seedUserRoles(string User, string Tech, string Admin, string Root, string User2, string User3, ApplicationDbContext db)
 {
     var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
     manager.AddToRole(User, Roles.CUSTOMER);
     manager.AddToRoles(Tech, Roles.TECH, Roles.CUSTOMER);
     manager.AddToRoles(Admin, Roles.ADMIN, Roles.TECH, Roles.CUSTOMER);
     manager.AddToRoles(Root, Roles.ROOT, Roles.ADMIN, Roles.TECH, Roles.CUSTOMER);
     manager.AddToRole(User2, Roles.CUSTOMER);
     manager.AddToRole(User3, Roles.CUSTOMER);
 }
        public async Task<IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var createUser = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

            var user = new ApplicationUser()
            {
                UserName = createUserModel.Username,
                Email = createUserModel.Email,
                FirstName = createUserModel.FirstName,
                LastName = createUserModel.LastName,
                Level = 3,
                JoinDate = DateTime.Now.Date,
            };

            IdentityResult addUserResult = await this.AppUserManager.CreateAsync(user, createUserModel.Password);

            if (!addUserResult.Succeeded)
            {
                return GetErrorResult(addUserResult);
            }

            string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

            var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));

            await this.AppUserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

            Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));

            var adminUser = createUser.FindByName(user.UserName);

            createUser.AddToRoles(adminUser.Id, new string[] { "User" });
            
            return Created(locationHeader, TheModelFactory.Create(user));
        }