예제 #1
0
        public async Task <ActionResult <UserModel> > PostLogin(UserModel user)
        {
            try
            {
                var userEntity = await _repository.GetUserByEmailAsync(user.Email);

                if (userEntity == null)
                {
                    return(NotFound("User Not Found!"));
                }

                var hasher = new PasswordHasher(user.Password);
                if (hasher.Compare(userEntity.Password))
                {
                    return(_mapper.Map <UserModel>(userEntity));
                }
                else
                {
                    return(Unauthorized("Incorrect password!"));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to login User!"));
            }
        }
예제 #2
0
        public ActionResult Login([FromBody] Login login)
        {
            if (dbContext.User.Any(e => e.Email == login.Email))
            {
                User user = dbContext.User.First(e => e.Email == login.Email);
                if (PasswordHasher.Compare(user.Password, login.Password, user.Salt))
                {
                    Request.HttpContext.Response.Headers.Add("Authentication", JwtBearerDefaults.AuthenticationScheme + " " + TokenService.GenerateToken(user));
                    return(StatusCode((int)HttpStatusCode.OK));
                }
                else
                {
                    return(StatusCode((int)HttpStatusCode.Unauthorized));
                }
            }
            else
            {
                return(StatusCode((int)HttpStatusCode.NotFound));
            }

            //return StatusCode((int)HttpStatusCode.ServiceUnavailable);
        }