public async Task <HttpResponseMessage> PostAsync([FromBody] AuthorizationViewModel authVM) { // check if the logIn user exists var error = await ValidationService.ValidateLogInUser(authVM.UserName, authVM.Password); if (error != null) { ModelState.AddModelError("Username", error.description); } if (!ModelState.IsValid) { // if there is no user with authVM parameters return empty result return(Request.CreateResponse(HttpStatusCode.OK)); } // logging in UserLogInDTO userLogInDTO = AMapper.Mapper.Map <AuthorizationViewModel, UserLogInDTO>(authVM); UserGetDetailDTO userGetDTO = await UserService.LogIn(userLogInDTO); // set auth and userCookie FormsAuthentication.SetAuthCookie(userGetDTO.UserName, true); var response = Request.CreateResponse <int>(HttpStatusCode.OK, userGetDTO.UserID); var cookie = UserCookieHelper.CreateUserCookie(userGetDTO); response.Headers.AddCookies(new CookieHeaderValue[] { cookie }); return(response); }
public async Task <UserTokenDTO> LogIn(UserLogInDTO userLogin) { var user = await Database.UserRepository.LogIn(userLogin.Email, userLogin.Password); if (user != null) { var mappedUser = mapper.Map <User, UserProfileDTO>(user); return(TokenManager.BuildToken(mappedUser)); } return(null); }
/// <summary> /// get UserLoginDTO by username and password /// </summary> /// <param name="userLogInDTO"></param> /// <returns></returns> public async Task <UserGetDetailDTO> LogIn(UserLogInDTO userLogInDTO) { // find users by UserName // delete after making ValidationService User userEntity = await UserRepository.GetByUserName(userLogInDTO.UserName); var userGetDetailDTO = AMapper.Mapper.Map <User, UserGetDetailDTO>(userEntity); // add record about signing in the user var userAction = new UserActionCreateDTO(userGetDetailDTO.UserID, UserActionType.LogIn); await UserActionService.InsertUserActionToDB(userAction); return(userGetDetailDTO); }
public async Task <UserTokenDTO> LogIn(UserLogInDTO user) { string serializedUser = JsonConvert.SerializeObject(user); var requestMessage = new HttpRequestMessage(HttpMethod.Post, "Login"); requestMessage.Content = new StringContent(serializedUser); requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = await httpClient.SendAsync(requestMessage); var responseStatusCode = response.StatusCode; var responseBody = await response.Content.ReadAsStringAsync(); var token = JsonConvert.DeserializeObject <UserTokenDTO>(responseBody); return(token != null ? token : null); }
public IActionResult Login(UserLogInDTO loginUser) { var user = new UserLogInDTO( loginUser.Email, loginUser.PasswordHash ); if (!ModelState.IsValid) { return(BadRequest("That user does not exist!")); } var userdb = Context.Users.FirstOrDefault(u => (u.Email == user.Email)); if (userdb == null) { return(Unauthorized()); } bool isPwCorrect = PasswordHasher.VerifyPassword(user.PasswordHash, userdb.PasswordHash); if (!isPwCorrect) { return(Unauthorized()); } string accessToken = AccessTokenGenerator.GenerateToken(userdb); Response.Cookies.Append("accessToken", accessToken, new CookieOptions { HttpOnly = true }); return(Ok(new { message = "success" })); }