Ejemplo n.º 1
0
        public async Task <IActionResult> Create(Staff staff)
        {
            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    UserName = staff.adEmail, Email = staff.adEmail
                };
                var result = await _userManager.CreateAsync(user, staff.password);

                if (!result.Succeeded)
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                        await ViewDataAddressesAndStores();

                        return(View(staff));
                    }
                }
                user = await _userManager.FindByNameAsync(staff.adEmail);

                await _userManager.AddToRoleAsync(user, staff.isAdmin? "Admin" : "Employee");

                staff.idUser = user.Id;
                await staffBusiness.saveStaff(staff);

                return(RedirectToAction(nameof(Index)));
            }
            await ViewDataAddressesAndStores();

            return(View(staff));
        }