public async Task <IActionResult> Create(Profile obj, string roleName, int tariffId) { await ServerSideValidation(obj, roleName, tariffId); if (ModelState.IsValid) { obj.Tariff = await DbContext.Tariffs .FirstOrDefaultAsync(t => t.Id.Equals(tariffId)); var result = await UserMngr.CreateAsync(obj.User); if (result.Succeeded) { var role = await RoleMngr.FindByNameAsync(roleName); await UserMngr.AddToRoleAsync(obj.User, role.Name); } await DbContext.Profiles.AddAsync(obj); await DbContext.SaveChangesAsync(); return(RedirectToAction("Index")); } return(await AddUpdateForm()); }
public async Task <IActionResult> Register(RegisterViewModel registerViewModel) { try { TempData["Message"] = "Użytkownik już istnieje!"; TempData["MessageType"] = "error"; if (!ModelState.IsValid) { return(View()); } AppUser appUser = new AppUser(); appUser.UserName = registerViewModel.UserName; appUser.Email = registerViewModel.Email; appUser.FirstName = "John"; appUser.LastName = "Doe"; IdentityResult result = await UserMngr.CreateAsync(appUser, registerViewModel.Password); if (result.Errors.Count() > 0) { foreach (IdentityError error in result.Errors) { ViewBag.Message += error.Description + "\n"; ViewBag.MessageType = "error"; } return(View()); } TempData["MessageType"] = "success"; TempData["Message"] = "Stworzono użytkownika!"; } catch (Exception ex) { ViewBag.Message = ex.Message; ViewBag.MessageType = "error"; return(View()); } return(RedirectToAction("Login")); }