public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser1 { UserName = model.UserName, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); //Assign Role to user Here await this.UserManager.AddToRoleAsync(user.Id, model.UserRoles); //Ends Here return(RedirectToAction("Index", "Users")); } ViewBag.Name = new SelectList(context.Roles.Where(u => !u.Name.Contains("Admin")) .ToList(), "Name", "Name"); AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult Edit(ApplicationUser1 user) { var userInDb = db.Users.Find(user.Id); if (userInDb == null) { return(View(user)); } if (ModelState.IsValid) { //userInDb.UserName = user.UserName; //userInDb.Age = user.Age; userInDb.Phone = user.Phone; userInDb.Email = user.Email; userInDb.UserName = user.UserName; db.Users.AddOrUpdate(userInDb); db.SaveChanges(); return(RedirectToAction("Index", "ManagerStaffViewModels")); } return(View(user)); }
private void createRolesandUsers() { ApplicationDbContext1 context = new ApplicationDbContext1(); var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var UserManager = new UserManager <ApplicationUser1>(new UserStore <ApplicationUser1>(context)); // In Startup iam creating first Admin Role and creating a default Admin User if (!roleManager.RoleExists("Admin")) { // first we create Admin role var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Admin"; roleManager.Create(role); } var user = new ApplicationUser1(); user.UserName = "******"; user.Email = "*****@*****.**"; string userPassword = "******"; var checkUser = UserManager.Create(user, userPassword); if (checkUser.Succeeded) { var result1 = UserManager.AddToRole(user.Id, "Admin"); } // creating Creating TrainingStaff role if (!roleManager.RoleExists("Staff")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Staff"; roleManager.Create(role); } // creating Creating Trainer role if (!roleManager.RoleExists("Trainer")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Trainer"; roleManager.Create(role); } // creating Creating Trainee role if (!roleManager.RoleExists("Trainee")) { var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Trainee"; roleManager.Create(role); } }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Manage")); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } var user = new ApplicationUser1 { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public ActionResult Delete(ApplicationUser1 user) { var userInDb = _context.Users.Find(user.Id); if (userInDb == null) { return(View(user)); } if (ModelState.IsValid) { userInDb.UserName = user.UserName; userInDb.Age = user.Age; userInDb.Phone = user.Phone; userInDb.Email = user.Email; _context.Users.Remove(userInDb); _context.SaveChanges(); return(RedirectToAction("UsersWithRoles")); } return(View(user)); }