public async Task <ActionResult> Register(TeacherRegisterViewModel model) { if (ModelState.IsValid) { var user = new BL.ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); TeacherData teacher = new TeacherData(model.FirstName, model.LastName, model.Email, model.PhoneNumber, model.Password); BL.RoadScholarContext rsContext = new BL.RoadScholarContext(); rsContext.addTeacher(teacher); // 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>"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
[Authorize(Roles = GlobalConstants.AdministratorRoleName)]//only admins can add users public ActionResult Register() { if (!User.IsInRole(GlobalConstants.AdministratorRoleName))//only admins can see the registration screen { return(RedirectToAction("Index", "Home", new { area = string.Empty })); } TeacherRegisterViewModel model = new TeacherRegisterViewModel(); return(View(model)); }
public async Task <ActionResult> Register(TeacherRegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email, PhoneNumber = model.PhoneNumber, }; IdentityResult result = await UserManager.CreateAsync(user, model.Password);//create the applicationUser through userManager if (result.Succeeded) { this.UserManager.AddToRole(user.Id, GlobalConstants.TeacherRoleName);//assign role of Teacher to newly registered user Teacher Teacher = new Teacher(); Teacher.ApplicationUserId = user.Id; Teacher.Name = model.Name; //Teacher.ApplicationUser = user; //Mapper.Map<RegisterViewModel, Teacher>(model, Teacher);//dump the model into the Teacher (in this case, all it does is Teacher.Name = model.Name but in other cases it will apare us a lot of lines of code) //the map is created in frontend/automapperconfig/organizationprofile.cs, here it is just applied. MichtavaResult res = this.teacherService.Add(Teacher);//add Teacher to DB through service if (res is MichtavaSuccess) { return(RedirectToAction("Index", "Teachers", new { area = "Administration" }));//TODO make this work } // return RedirectToAction("Index", "Home", new { area = string.Empty });//getting 404 here else { ModelState.AddModelError(string.Empty, res.Message); } } else { this.AddErrors(result); } } // If we got this far, something failed, redisplay form return(View(model)); }