Exemplo n.º 1
0
        public async Task <IActionResult> RegisterUser([FromBody] CreateUserRequestModel createUserRequestModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var masterResult = Repo.GetUserMaster(createUserRequestModel.UserEmail);

            if (masterResult != null)
            {
                return(Ok("Exists"));
            }

            var userMaster = Mapper.Map <UserMaster>(createUserRequestModel);

            (userMaster.Salt, userMaster.UserPassword) =
                _passwordHashingHelper.GetHashedPassword(createUserRequestModel.UserPassword);

            if (userMaster.Salt == null || userMaster.UserPassword == null)
            {
                return(StatusCode(500));
            }

            await Repo.AddDataToDataSet(userMaster);

            var result = Repo.SaveData();

            if (result != 1)
            {
                Logger.LogWarning("Failed to save data in UserMaster table");
                return(BadRequest("Could not input user data"));
            }

            createUserRequestModel = Mapper.Map(userMaster, createUserRequestModel);
            var userDetails = Mapper.Map <UserDetails>(createUserRequestModel);

            userDetails.UserId = userMaster.UserId;
            await Repo.AddDataToDataSet(userDetails);

            result = Repo.SaveData();
            if (result == 1)
            {
                var token = TokenHandler.Create(createUserRequestModel.UserEmail, AppSettings.Value.SigningKey);
                if (string.IsNullOrEmpty(token))
                {
                    return(StatusCode(500));
                }

                createUserRequestModel = Mapper.Map(userDetails, createUserRequestModel);
                var returnObject = Mapper.Map <CreateUserResponseModel>(createUserRequestModel);
                returnObject.Token = token;
                return(Created("api/users", returnObject));
            }

            Logger.LogWarning("Failed to save data in user details table");
            return(BadRequest("could not add a valid value. \n"));
        }
Exemplo n.º 2
0
        public IActionResult CreateToken([FromBody] TokenRequestModel requestModelModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var details = Repo.GetUserMaster(requestModelModel.UserEmail);

            if (details == null)
            {
                return(BadRequest("User validation failed"));
            }

            var hashedPassword = _passwordHashingHelper.HashValues(requestModelModel.Password, _passwordHashingHelper.GetSaltFromString(details.Salt));

            if (!string.Equals(hashedPassword, details.UserPassword))
            {
                return(BadRequest("User validation failed"));
            }

            var token = TokenHandler.Create(requestModelModel.UserEmail, AppSettings.Value.SigningKey);

            if (string.IsNullOrEmpty(token))
            {
                return(StatusCode(500));
            }

            var tokenModel = new TokenResponseModel()
            {
                UserEmail   = requestModelModel.UserEmail,
                BearerToken = token
            };

            return(Ok(tokenModel));
        }