public MembershipUser CreateUser(string email, string password) { MembershipUser membershipUser = GetUser(email, false); if (membershipUser == null) { try { using (DataContext _db = new DataContext()) { User user = new User(); user.Email = email; user.Password = Crypto.HashPassword(password); if (_db.Roles.Find(2) != null) { user.RoleID = 2; } _db.Users.Add(user); _db.SaveChanges(); membershipUser = GetUser(email, false); return membershipUser; } } catch { return null; } } return null; }
/// <summary> /// Creates default admin user. /// </summary> private async Task CreateAdminUser() { var user = await userManager.FindByNameAsync(settings.DefaultAdminUserEmail); if (user != null) return; user = new User { UserName = settings.DefaultAdminUserEmail, Email = settings.DefaultAdminUserEmail, FirstName = "Admin", LastName = "Admin", CreateDate = DateTime.Now }; var result = await userManager.CreateAsync(user, settings.DefaultAdminUserPassword); if (!result.Succeeded) throw new Exception("Error creating default admin user: "******"Error adding default admin user to the role: " + result.Errors.FirstOrDefault().Description); //await userManager.AddClaimAsync(user, new Claim("ManageStore", "Allowed")); }
public User CreateUserWithCompany() { var company = new Company() { Id = 1, Name = "testCompany1" }; var user = new User() { Id = 1, CreateDate = DateTime.Now, Disabled = false, Email = "*****@*****.**", FirstName = "testUser", LastName = "testUser", Password = userService.HashPassword("12345"), Role = UserRoles.Member, Company = company }; return user; }
public ActionResult Register(RegisterViewModel user) { var roles = roleService.GetMany(s => s.Id < 3).ToList(); user.Roles = roles; try { if (ModelState.IsValid && ReCaptcha.Validate(ConfigurationManager.AppSettings["ReCaptcha:SecretKey"])) { var model = new User(); model.Firstname = user.Firstname; model.Lastname = user.Lastname; model.Age = user.Age; model.Email = user.Email; model.Password = Crypto.HashPassword(user.Password); model.RoleID = user.RoleId; model.City = user.City; model.Address = user.Address; model.CreationDate = DateTime.Now; userService.Add(model); ViewBag.Message = "Paldies. Reģistrācija noritēja veiksmīgi"; return RedirectToAction("Index", "Home"); } ViewBag.RecaptchaLastErrors = ReCaptcha.GetLastErrors(this.HttpContext); ViewBag.Recaptcha = ReCaptcha.GetHtml(ConfigurationManager.AppSettings["ReCaptcha:SiteKey"], "light", "image"); } catch (Exception) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError); } return View(user); }
public IActionResult CreateUser(UserFormViewModel model) { if (ModelState.IsValid) { var newUser = new User(); newUser.UserName = model.Email; newUser.CompanyId = model.CompanyId; newUser.FirstName = model.FirstName; newUser.LastName = model.LastName; newUser.Email = model.Email; newUser.Disabled = model.Disabled; newUser.CreateDate = DateTime.Now; var result = userManager.CreateAsync(newUser, model.Password).Result; if (result.Succeeded) { result = userManager.AddToRoleAsync(newUser, RoleNames.User).Result; if (result.Succeeded) return Json(new { success = true }); } CollectIdentityErrors(result); } var m = new UserFormViewModel(companyService.GetAll().ToList()); return PartialView(m); }
public async Task<IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null) { if (User.IsSignedIn()) { return RedirectToAction(nameof(ManageController.Index),"Manage"); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } var user = new User { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent: false); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewData["ReturnUrl"] = returnUrl; return View(model); }
public async Task<IActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new User() { UserName = model.Email, Email = model.Email, CreateDate = DateTime.Now }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { result = await _userManager.AddToRoleAsync(user, RoleNames.User); if (result.Succeeded) { // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713 // Send an email with this link //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); //await _emailSender.SendEmailAsync(model.Email, "Confirm your account", // "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"); await _signInManager.SignInAsync(user, isPersistent: false); return RedirectToAction(nameof(HomeController.Index), "Home"); } } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
public void Test_EditUser_View_Post() { var updatedUser = new User(); var user = CreateUserWithCompany(); userService.Stub(x => x.GetById(1)).Return(user); userService.Expect(x => x.Update(user)).WhenCalled(c => updatedUser = user); companyService.Stub(x => x.GetById(2)).Return(new Company() { Id = 2, Name = "TestCompany2" }); var editUserViewModel = new EditUserViewModel() { Id = 1, CompanyId = 2, Disabled = true, Email = "*****@*****.**", FirstName = "testUser2", LastName = "testUser2" }; var adminController = new AdminController(userService, companyService, orderService); var result = (RedirectToRouteResult)adminController.EditUser(editUserViewModel); Assert.AreEqual(updatedUser.Email, "*****@*****.**"); Assert.AreEqual(updatedUser.FirstName, "testUser2"); Assert.AreEqual(updatedUser.LastName, "testUser2"); Assert.AreEqual(updatedUser.Company.Name, "TestCompany2"); Assert.IsTrue(updatedUser.Disabled); Assert.AreEqual("UserList", result.RouteValues["Action"]); }
public ActionResult Register(RegisterViewModel model) { if (!ModelState.IsValid) return View(model); var company = companyService.GetById(model.CompanyId); var user = new User(); user.Company = company; user.FirstName = model.FirstName; user.LastName = model.LastName; user.Email = model.Email; user.Password = userService.HashPassword(model.ConfirmPassword); user.Role = UserRoles.Member; userService.Add(user); FormsAuthentication.SetAuthCookie(model.Email, false); return RedirectToAction("Index", "Home"); }