public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { try { var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, Hometown = model.Hometown , InstitutionID = model.InstitutionID}; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent: 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>"); return RedirectToAction("Index", "Home"); } else { AddErrors(result); model.Institutions = from value in db.Institutions select new SelectListItem { Text = value.FullName, Value = value.ID.ToString(), Selected = false }; } } catch (DbEntityValidationException e) { StringBuilder message = new StringBuilder(); foreach (var eve in e.EntityValidationErrors) { message.AppendFormat("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { message.AppendFormat("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw new Exception(message.ToString()); } } // If we got this far, something failed, redisplay form return View(model); }
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return RedirectToAction("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 ApplicationUser() { UserName = model.Email, Email = model.Email, Hometown = model.Hometown }; IdentityResult result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInAsync(user, isPersistent: 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); // SendEmail(user.Email, callbackUrl, "Confirm your account", "Please confirm your account by clicking this link"); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }
private async Task SignInAsync(ApplicationUser user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager)); }
public async Task<ActionResult> Create(RegisterViewModel userViewModel, params string[] selectedRoles) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = userViewModel.UserName, Email = userViewModel.Email, InstitutionID = userViewModel.InstitutionID, FirstName1 = userViewModel.FirstName1, FirstName2 = userViewModel.FirstName2, LastName1 = userViewModel.LastName1, LastName2 = userViewModel.LastName2, Hometown = userViewModel.Hometown, }; var adminresult = await UserManager.CreateAsync(user, userViewModel.Password); userViewModel.Institutions = from value in db.Institutions select new SelectListItem { Text = value.FullName, Value = value.ID.ToString(), Selected = false }; userViewModel.Institutions = userViewModel.Institutions.OrderBy(o => o.Text); //Get the list of Roles userViewModel.RolesList = RoleManager.Roles.ToList().Select(x => new SelectListItem() { Selected = selectedRoles.Contains(x.Name), Text = x.Name, Value = x.Name }); //Add User to the selected Roles if (adminresult.Succeeded) { if (selectedRoles != null) { var result = UserManager.AddToRolesAsync(user.Id, selectedRoles); if (!result.Succeeded) { ModelState.AddModelError("", result.Errors.First()); ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name"); return View(userViewModel); } } } else { ModelState.AddModelError("", adminresult.Errors.First()); ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name"); return View(userViewModel); } return RedirectToAction("Index"); } ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name"); return View(userViewModel); }