public bool VerifyUserLogin(IUser user)
        {
            ApiCreateUser apiUser = new ApiCreateUser();

            apiUser.Email    = user.Email;
            apiUser.Password = user.Password;

            bool userVerfied;

            userVerfied = this.APIVerfiyUser(apiUser).Result;

            return(userVerfied);
        }
        public IUser Create(IUser user)
        {
            ApiCreateUser apiUser = new ApiCreateUser();

            apiUser.Email    = user.Email;
            apiUser.Username = user.Username;
            apiUser.Password = user.Password;
            apiUser.Salt     = user.Salt;

            IUser returnUser;

            returnUser = this.PutIUser(apiUser).Result;

            return(returnUser);
        }
        private async Task <IUser> PutIUser(ApiCreateUser apiUser)
        {
            var        content = JsonConvert.SerializeObject(apiUser);
            HttpClient _client = new HttpClient();

            var httpResponse = await _client.PostAsync(this.apiString + "users", new StringContent(content, Encoding.Default, "application/json"));

            if (!httpResponse.IsSuccessStatusCode)
            {
                throw new Exception("Cannot create a user");
            }

            var createdTask = JsonConvert.DeserializeObject <User>(await httpResponse.Content.ReadAsStringAsync());

            return(createdTask);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Post([FromBody] ApiCreateUser userCreateModel)
        {
            try
            {
                var user = _mapper.Map <User>(userCreateModel);
                await userService.AddUserSignUp(user);

                return(CreatedAtRoute(
                           routeName: "Get",
                           routeValues: new { id = user.Id },
                           value: new ApiUser {
                    FirstName = user.FirstName, LastName = user.LastName, Email = user.Email
                }));
            }
            //Should be handled by global exception handler
            catch (ValidateException ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Post([FromBody] ApiCreateUser newUser)
        {
            int    d, y, m;
            string path = configuration.GetSection("LogPath").Value;
            Logger log  = new Logger(path);

            bool dd = int.TryParse(newUser.Day.Trim(), out d);
            bool mm = int.TryParse(newUser.Month.Trim(), out m);
            bool yy = int.TryParse(newUser.Year.Trim(), out y);

            if (!dd || !mm || !yy)
            {
                return(BadRequest(new { error = "Invalid day/month/year" }));
            }

            DateTime dob;

            try
            {
                dob = new DateTime(y, m, d);
            }
            catch
            {
                return(BadRequest(new { error = "Invalid date" }));
            }

            try
            {
                MailAddress email = new MailAddress(newUser.Email.Trim());
            }
            catch (FormatException fme)
            {
                return(BadRequest(new { error = "Invalid e-mail" }));
            }
            catch
            {
                return(BadRequest(new { error = "E-mail cannot be null." }));
            }

            if (!newUser.PhoneNumber.Trim().StartsWith("0"))
            {
                return(BadRequest(new { error = "Phone number must start with 0." }));
            }
            else if (newUser.PhoneNumber.Trim().Length != 11)
            {
                return(BadRequest(new { error = "Phone number must be 11 digits." }));
            }

            try
            {
                if (Convert.ToChar(newUser.Sex.Trim().ToUpper()) != 'M' && Convert.ToChar(newUser.Sex.ToUpper().Trim()) != 'F')
                {
                    return(BadRequest(new { error = "Sex must be M or F." }));
                }
            }
            catch
            {
                return(BadRequest(new { error = "Invalid. Sex must be M or F." }));
            }

            if (newUser.UserName.Trim().Length < 5 || newUser.UserName.Trim().Length > 10)
            {
                return(BadRequest(new { error = "Username must have between 5 and 10 characters." }));
            }

            if (newUser.FirstName.Trim().Length < 2)
            {
                return(BadRequest(new { error = "First name must have at least 2 characters." }));
            }

            if (newUser.LastName.Trim().Length < 2)
            {
                return(BadRequest(new { error = "Last name must have at least 2 characters." }));
            }

            AppUser user = new AppUser
            {
                UserName    = newUser.UserName.Trim(),
                Email       = newUser.Email.Trim(),
                PhoneNumber = newUser.PhoneNumber.Trim(),
                FirstName   = newUser.FirstName.Trim(),
                LastName    = newUser.LastName.Trim(),
                DoB         = dob,
                Sex         = newUser.Sex.ToUpper().Trim()
            };

            try
            {
                IdentityResult identityResult = await userStore.CreateNewUser(user, newUser.Password);

                if (identityResult.Succeeded)
                {
                    AppUser u = await userStore.FindUser(user.UserName);

                    user = new AppUser
                    {
                        Id          = u.Id,
                        UserName    = u.UserName,
                        Email       = u.Email,
                        PhoneNumber = u.PhoneNumber,
                        FirstName   = u.FirstName,
                        LastName    = u.LastName,
                        DoB         = u.DoB,
                        Sex         = u.Sex
                    };
                    return(Ok(user));
                }
                else
                {
                    IEnumerable <IdentityError> errs = identityResult.Errors;
                    foreach (IdentityError err in errs)
                    {
                        log.WriteError(new string[] { $"Error code: {err.Code}", $"Error description: {err.Description}" });
                    }

                    return(BadRequest(errs));
                }
            }
            catch (Exception ex)
            {
                log.WriteError(new string[] { $"Exception msg: {ex.Message}", $"Exception stack trace: {ex.StackTrace}" });
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }