private static async Task <string> EnsureUser(IServiceProvider serviceProvider, string testUserPw, string UserName, int ProfileID, bd_lesContext _context) { var userManager = serviceProvider.GetService <UserManager <ApplicationUsers> >(); var func = new Funcionario { NomeCompleto = UserName }; var user = await userManager.FindByNameAsync(UserName); if (user == null && UserName != null && ProfileID != 0) { _context.Add(func); await _context.SaveChangesAsync(); user = new ApplicationUsers { UserName = UserName, FuncionarioIdFuncionario = func.IdFuncionario, IdPerfil = ProfileID }; var result = await userManager.CreateAsync(user, testUserPw); if (!result.Succeeded) { Console.WriteLine(result.Errors); _context.Remove(func); await _context.SaveChangesAsync(); } } return(user.Id); }
public async Task <IActionResult> Create(RegisterViewModel registerViewModel) { var user2 = await _userManager.FindByNameAsync(registerViewModel.UserName); if (user2 != null) { ModelState.AddModelError("UserName", string.Format("Já existe um utilizador com o nome {0}.", registerViewModel.UserName)); } if (ModelState.IsValid) { var func = new Funcionario { NomeCompleto = registerViewModel.NomeCompleto }; if (registerViewModel.NomeCompleto != null) { _context.Add(func); await _context.SaveChangesAsync(); } var user = new ApplicationUsers { UserName = registerViewModel.UserName, PhoneNumber = registerViewModel.PhoneNumber, FuncionarioIdFuncionario = func.IdFuncionario, IdPerfil = registerViewModel.IdPerfil }; var result = await _userManager.CreateAsync(user, registerViewModel.Password); if (result.Succeeded) { List <ProfileRole> roles = await _context.ProfileRole .Where(p => p.IdPerfil == user.IdPerfil) .ToListAsync(); foreach (ProfileRole role in roles) { string role_name = _roleManager.FindByIdAsync(role.RoleId).Result.Name; if (await _roleManager.RoleExistsAsync(role_name)) { await _userManager.AddToRoleAsync(user, role_name); } } return(RedirectToAction(nameof(Index))); } else { _context.Remove(func); await _context.SaveChangesAsync(); } } ViewData["IdPerfil"] = new SelectList(_context.Perfil, "IdPerfil", "NomePerfil").Prepend(new SelectListItem() { Text = "---Selecione um Perfil---", Value = "" }); return(View(registerViewModel)); }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var func = new Funcionario { NomeCompleto = model.NomeCompleto }; if (model.NomeCompleto != null) { _context.Add(func); await _context.SaveChangesAsync(); } var default_profile = await _context.Perfil.SingleOrDefaultAsync(p => p.IsDefault == 1); var user = new ApplicationUsers { UserName = model.UserName, PhoneNumber = model.PhoneNumber, FuncionarioIdFuncionario = func.IdFuncionario, IdPerfil = (model.IdPerfil == 0) ? default_profile.IdPerfil : model.IdPerfil }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); //await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created a new account with password."); return(RedirectToLocal(returnUrl)); } else { _context.Remove(func); await _context.SaveChangesAsync(); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }