Esempio n. 1
0
        //POST : /api/AppUsers/Register
        public async Task <Object> PostApplicationUser(TOAppUser model)
        {
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user != null)
            {
                return(Ok("Email is allready taken"));
                //return StatusCode(400, "Email is already taken");
            }

            var applicationUser = new AppUser()
            {
                UserName     = model.UserName,
                Email        = model.Email,
                Name         = model.Name,
                Surname      = model.Surname,
                PhoneNumber  = model.PhoneNumber,
                City         = model.City,
                Company      = model.Company,
                IsFirstLogIn = true
            };

            try
            {
                var result = await _userManager.CreateAsync(applicationUser, model.Password);

                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(applicationUser, model.Role);

                    string token = await _userManager.GenerateEmailConfirmationTokenAsync(applicationUser);

                    MailingService.SendEmailVerification(applicationUser, model.Role, token, model.Password);
                }

                return(Ok(result));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }