Beispiel #1
0
        public async Task <ActionResult <User> > AddUser(UserViewModel input)
        {
            var    user         = input.Adapt <User>();
            string userPassword = user.Password;

            if (string.IsNullOrEmpty(user.Password))
            {
                //GENERATES PASSWORD
                userPassword = _accountsServices.CreatePassword(8);
            }

            //ENCRYPTS PASSWORD
            user.Password         = Helpers.StringCipher.Encrypt(userPassword);
            user.IsFirstTimeLogin = true;
            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            try
            {
                //SEND WELCOME EMAIL WITH PASSWORD
                string to = user.Email, userID, password, SMTPPort, host;

                _emailServices.AppSettings(out userID, out password, out SMTPPort, out host);

                var    url     = this._configuration["webSiteUrl"];
                string subject = "Thaniyas Farmer App Account Created";
                string body    = @"Welcome to Thaniyas Farmer App!<br /><br />

                                        You have been added to the Thaniyas Farmer App system found here: " + url + @"<br /><br />

                                        Please login with the below username and temporary password.<br /><br />

                                        Username: "******"<br />
                                        Password: "******"<br />";


                _emailServices.SendEmail(subject, body, to, userID, password, SMTPPort, host);
            }
            catch (Exception ex)
            {
            }

            return(CreatedAtAction("GetUser", new { id = user.ID }, user));
        }