public IActionResult Create([FromBody] HrUserDetails hrUserDetails) { if (hrUserDetails == null) { return(BadRequest(ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_hrDetails.AddHr(hrUserDetails)) { ModelState.AddModelError("", $"Something went wrong with saving "); return(StatusCode(500, ModelState)); } return(CreatedAtRoute("HR", new { id = hrUserDetails.Id }, hrUserDetails)); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new ApplicationUser { FirstName = Input.FirstName, LastName = Input.LastName, UserName = Input.Email, Email = Input.Email, UserRole = Input.UserRole }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var userId = _userManager.Users.SingleOrDefault(i => i.Email == user.Email).Id; var role = Input.UserRole; switch (role) { case "Client": if (userId != null) { var client = new ClientUserDetails { ApplicationUserId = userId }; _clientRepository.AddClient(client); } break; case "Mentor": if (userId != null) { var mentor = new MentorUserDetails { ApplicationUserId = userId }; _mentorRepository.AddMentor(mentor); } break; case "HR": if (userId != null) { var HR = new HrUserDetails { ApplicationUserId = userId }; _hrRepository.AddHr(HR); } break; case "Admin": if (userId != null) { var admin = new AdministratorDetails { ApplicationUserId = userId }; _administratorDetailsRepository.AddAdministrator(admin); } break; } var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); //Προσθέτω μετά το Register το User στο Role εγγραφή στον ενδιάμεσο πίνακα Σπυροσσ await _userManager.AddToRoleAsync(user, Input.UserRole); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", new { email = Input.Email })); } else { await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError("Error", error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }