public ActionResult UnbanUser(DeleteUserVM deletedUser) { if (ModelState.IsValid) { var userStore = new UserStore <IdentityUser>(); UserManager <IdentityUser> manager = new UserManager <IdentityUser>(userStore); var currentUser = manager.FindByEmail(deletedUser.Email); OneListCAEntities context = new OneListCAEntities(); if (currentUser != null) { var user = context.AspNetUsers.Find(currentUser.Id); if (user.PhoneNumberConfirmed == true) { user.PhoneNumberConfirmed = false; context.SaveChanges(); SendGrid.sendUnbanUserEmail(currentUser.Email, currentUser.UserName); ViewBag.Success = "User has been unbanned successfully!"; } else { ViewBag.Fail = "This user is not banned!"; } } else { ViewBag.Fail = "User not found!"; } } return(View()); }
public ActionResult DeleteUser(DeleteUserVM deletedUser) { if (ModelState.IsValid) { var userStore = new UserStore <IdentityUser>(); UserManager <IdentityUser> manager = new UserManager <IdentityUser>(userStore); var currentUser = manager.FindByEmail(deletedUser.Email); OneListCAEntities context = new OneListCAEntities(); if (currentUser != null) { var user = context.AspNetUsers.Find(currentUser.Id); var userProfile = context.Users.Find(currentUser.Id); context.AspNetUsers.Remove(user); context.Users.Remove(userProfile); context.SaveChanges(); ViewBag.Success = "User has been deleted successfully!"; } else { ViewBag.Fail = "User not found!"; } } return(View()); }
public ActionResult AddUserToRole(UserRoleVM userRoleVM) { var userStore = new UserStore <IdentityUser>(); UserManager <IdentityUser> manager = new UserManager <IdentityUser>(userStore); if (ModelState.IsValid) { OneListCAEntities context = new OneListCAEntities(); AspNetUser user = context.AspNetUsers .Where(u => u.Email == userRoleVM.Email).FirstOrDefault(); if (userRoleVM.RoleName == "Administrator") { manager.RemoveFromRole(user.Id, "User"); manager.AddToRole(user.Id, userRoleVM.RoleName); } else if (userRoleVM.RoleName == "User") { manager.RemoveFromRole(user.Id, "Administrator"); manager.AddToRole(user.Id, userRoleVM.RoleName); } //user.AspNetRoles.Add(role); context.SaveChanges(); } return(View()); }
public ActionResult AddRole(RoleVM roleVM) { if (ModelState.IsValid) { AspNetRole role = new AspNetRole(); role.Id = roleVM.RoleName; role.Name = roleVM.RoleName; OneListCAEntities context = new OneListCAEntities(); context.AspNetRoles.Add(role); context.SaveChanges(); } return(View()); }
public ActionResult Register(RegisteredUserVM newUser) { var userStore = new UserStore <IdentityUser>(); UserManager <IdentityUser> manager = new UserManager <IdentityUser>(userStore) { UserLockoutEnabledByDefault = true, DefaultAccountLockoutTimeSpan = new TimeSpan(0, 10, 0), MaxFailedAccessAttemptsBeforeLockout = 5 }; var identityUser = new IdentityUser() { UserName = newUser.UserName, Email = newUser.Email }; if (ModelState.IsValid) { CaptchaHelper captchaHelper = new CaptchaHelper(); OneListCAEntities context = new OneListCAEntities(); string captchaResponse = captchaHelper.CheckRecaptcha(); if (captchaResponse == "Valid") { if (manager.FindByEmail(newUser.Email) == null) { ViewBag.CaptchaResponse = captchaResponse; IdentityResult result = manager.Create(identityUser, newUser.Password); if (result.Succeeded) { AspNetUser user = context.AspNetUsers .Where(u => u.UserName == newUser.UserName).FirstOrDefault(); AspNetRole role = new AspNetRole(); role.Id = "User"; role.Name = "User"; user.AspNetRoles.Add(context.AspNetRoles.Find(role.Id)); context.SaveChanges(); //add information of user and password to table users in core CreateTokenProvider(manager, EMAIL_CONFIRMATION); var code = manager.GenerateEmailConfirmationToken(identityUser.Id); var callbackUrl = Url.Action("ConfirmEmail", "Home", new { userId = identityUser.Id, code = code }, protocol: Request.Url.Scheme); //string email = "Please confirm your account by clicking this link: <a href=\"" // + callbackUrl + "\">Confirm Registration</a>"; SendGrid.sendEmail(newUser, callbackUrl); ViewBag.Result = "Please check your email to activate your account!"; } else { ViewBag.Result = "User already exist!"; } } else { ViewBag.Result = "User already exist!"; } } else { ViewBag.Result = "Registration failed!"; } } return(View()); }