public async Task <IActionResult> Login(Credentials credentials) { var userProfile = _userProfileRepository.GetByEmail(credentials.Email); if (userProfile == null) { ModelState.AddModelError("Email", "Invalid email"); return(View()); } if (userProfile.Active) { var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, userProfile.Id.ToString()), new Claim(ClaimTypes.Email, userProfile.Email), new Claim(ClaimTypes.Role, userProfile.UserTypeId.ToString()) }; var claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme); await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)); return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("Email", "Inactive user"); return(View()); } }
public IActionResult ExistingUserCheck(string email, string displayName) { var userProfile = _userProfileRepository.GetByEmail(email, displayName); if (userProfile == null) { return(NotFound()); } return(Ok(userProfile)); }
public async Task <IActionResult> Register(UserProfile userProfile) { try { userProfile.CreateDateTime = DateAndTime.Now; userProfile.UserTypeId = 1; var userProfileCheck = _userProfileRepository.GetByEmail(userProfile.Email); if (userProfileCheck != null) { ModelState.AddModelError("Email", "Please enter unique email"); return(View()); } _userProfileRepository.AddNewUser(userProfile); var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, userProfile.Id.ToString()), new Claim(ClaimTypes.Email, userProfile.Email), new Claim(ClaimTypes.Role, userProfile.UserTypeId.ToString()) }; var claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme); await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)); return(RedirectToAction("Index", "Home")); } catch { return(View(userProfile)); } }
public async Task <IActionResult> Register(UserProfile newUser) { if (_userProfileRepository.GetByEmail(newUser.Email) == null) { try { newUser.CreateDateTime = DateAndTime.Now; newUser.UserTypeId = 2; _userProfileRepository.Add(newUser); } catch { return(View()); } var registeredUser = _userProfileRepository.GetByEmail(newUser.Email); var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, registeredUser.Id.ToString()), new Claim(ClaimTypes.Email, registeredUser.Email), new Claim(ClaimTypes.Role, registeredUser.UserType.Name) }; var claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme); await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)); return(RedirectToAction("Index", "Home")); } return(View()); }