public static Response Create(RegisterModel registerModel) { var response = new Response(); var context = new AdminDbContext(); if (context.Users.Any(u => u.Email.Equals(registerModel.Email))) { response.AddMessage(false, "This email is already registered", ResponseMessageType.Warning); } else { var user = new User(); user.Email = registerModel.Email; user.Password = GenerateHashWithSalt(registerModel.Password, registerModel.Email); user.PasswordSalt = Path.GetRandomFileName(); context.Users.Add(user); context.SaveChanges(); } return response; }
public static async Task<Response> CreateUserAsync(RegisterModel registerModel) { var response = new Response(); var context = new AdminDbContext(); if (context.Users.Any(u => u.Email.Equals(registerModel.Email))) { response.AddMessage(false, "This email is already registered", ResponseMessageType.Warning); } else { var user = new User(); user.Email = registerModel.Email; user.Password = GenerateHashWithSalt(registerModel.Password, registerModel.Email); user.PasswordSalt = Path.GetRandomFileName(); user.RegisterDate = DateTime.Now; user.Active = true; context.Users.Add(user); await context.SaveChangesAsync(); } return response; }
public async Task<ActionResult> Register(RegisterModel model) { if (ModelState.IsValid) { var result = await UserManager.CreateUserAsync(model); if (result.Success) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; 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>"); return RedirectToAction("Index", "Home"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }