Пример #1
0
        public IHttpActionResult RegisterUserProfile([FromBody] NSALK.Models.UserProfile.SystemDataMessage incoming)
        {
            Console.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
            NSALK.Models.UserProfile.SystemDataResponse outgoing = new Models.UserProfile.SystemDataResponse();
            if (incoming != null)
            {
                int memberId = Convert.ToInt32(User.Identity.GetUserId());
                foreach (var cts in incoming.Contacts)
                {
                    NSALK.Models.UserProfile.SystemDataResponseContacts contacts = new Models.UserProfile.SystemDataResponseContacts();

                    foreach (var ct in cts.Contact)
                    {
                        NSALK.Models.UserProfile.SystemDataResponseContact contact = new Models.UserProfile.SystemDataResponseContact();
                        using (NSAKLEntities context = new NSAKLEntities())
                        {
                            #region Register User
                            var usr = context.users.Where(x => x.username == ct.Username).FirstOrDefault();
                            if (usr != null)
                            {
                                FriendlyException.RaiseExeption("Login name already in use please choose another one", "Error", HttpStatusCode.PreconditionFailed);
                            }


                            string newPassword = "******";
                            if (!string.IsNullOrEmpty(ct.Password))
                            {
                                newPassword = ct.Password;
                            }
                            else
                            {
                                newPassword = NSALK.MvcApplication.RandomPassword.Generate(6);
                            }
                            string hashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(newPassword, "sha1");

                            context.members.Add(new member
                            {
                                //recid = ct.recid,
                                firstname  = !string.IsNullOrEmpty(ct.Firstname) ? ct.Firstname:"",
                                middlename = !string.IsNullOrEmpty(ct.Middlename) ? ct.Middlename:"",
                                lastname   = !string.IsNullOrEmpty(ct.Lastname) ? ct.Lastname:"",
                                gender     = !string.IsNullOrEmpty(ct.Gender) ? ct.Gender:"",
                                physical_address_unit_number      = !string.IsNullOrEmpty(ct.Physicaladdressunitnumber) ? ct.Physicaladdressunitnumber:"",
                                physical_address_number           = !string.IsNullOrEmpty(ct.Physicaladdressnumber) ? ct.Physicaladdressnumber:"",
                                physical_address_name             = !string.IsNullOrEmpty(ct.Physicaladdressname) ? ct.Physicaladdressname:"",
                                physical_address_suburb           = !string.IsNullOrEmpty(ct.Physicaladdresssuburb) ? ct.Physicaladdresssuburb:"",
                                physical_address_postcode         = !string.IsNullOrEmpty(ct.Physicaladdresspostcode) ? ct.Physicaladdresspostcode:"",
                                postal_address_number             = !string.IsNullOrEmpty(ct.Postaladdressnumber) ? ct.Postaladdressnumber:"",
                                postal_address_box_lobby_location = !string.IsNullOrEmpty(ct.Postaladdressboxlobbylocation) ? ct.Postaladdressboxlobbylocation:"",
                                postal_address_suburb             = !string.IsNullOrEmpty(ct.Postaladdresssuburb) ? ct.Postaladdresssuburb:"",
                                postal_address_postcode           = !string.IsNullOrEmpty(ct.Postaladdresspostcode) ? ct.Postaladdresspostcode:"",
                                phone_home              = !string.IsNullOrEmpty(ct.Phonehome) ? ct.Phonehome:"",
                                phone_work              = !string.IsNullOrEmpty(ct.Phonework) ? ct.Phonework:"",
                                phone_cell              = !string.IsNullOrEmpty(ct.Phonecell) ? ct.Phonecell:"",
                                email_address_default   = !string.IsNullOrEmpty(ct.Emailaddressdefault) ? ct.Emailaddressdefault:"",
                                email_address_alternate = !string.IsNullOrEmpty(ct.Emailaddressalternate) ? ct.Emailaddressalternate:"",
                                username        = !string.IsNullOrEmpty(ct.Username) ? ct.Username:"",
                                ip_address      = !string.IsNullOrEmpty(ct.Ipaddress) ? ct.Ipaddress:"",
                                remarks         = !string.IsNullOrEmpty(ct.Remarks) ? ct.Remarks:"",
                                occupation      = !string.IsNullOrEmpty(ct.Occupation) ? ct.Occupation:"",
                                other_remarks   = !string.IsNullOrEmpty(ct.Otherremarks) ? ct.Otherremarks:"",
                                last_updated_by = !string.IsNullOrEmpty(ct.Lastupdatedby) ? ct.Lastupdatedby:"",
                                signup_date     = !string.IsNullOrEmpty(ct.Signupdate) ? Convert.ToDateTime(ct.Signupdate) : DateTime.Now,
                                emergency_volunteering_status = !string.IsNullOrEmpty(ct.Emergencyvolunteeringstatus) ? Convert.ToBoolean(ct.Emergencyvolunteeringstatus): false,
                                mailing_list_status           = !string.IsNullOrEmpty(ct.Mailingliststatus) ? Convert.ToBoolean(ct.Mailingliststatus): false,
                                status            = !string.IsNullOrEmpty(ct.Status) ? Convert.ToBoolean(ct.Status):false,
                                birth_date        = !string.IsNullOrEmpty(ct.Birthdate) ? Convert.ToDateTime(ct.Birthdate) : new DateTime(1900, 01, 01),
                                last_updated_date = DateTime.Now
                            });

                            if (MvcApplication.isValidEmail(ct.Emailaddressdefault))
                            {
                                string errorMessage = "";
                                string email        = ct.Emailaddressdefault;
                                if (MvcApplication.isValidEmail(ct.Emailaddressdefault))
                                {
                                    email = ct.Emailaddressdefault;
                                }
                                else
                                if (MvcApplication.isValidEmail(ct.Emailaddressalternate))
                                {
                                    email = ct.Emailaddressalternate;
                                }


                                if (MvcApplication.SendEmail(email, "NSAKL Registration", "Rgistered Successfully. Your username is :" + ct.Username + Environment.NewLine + "Your new password is" + Environment.NewLine + newPassword, false, ref errorMessage))
                                {
                                    contact.Username = ct.Username;
                                    var user = context.users.Add(new user {
                                        username = ct.Username,
                                        password = hashedPassword
                                    });
                                    context.SaveChanges();
                                }
                                else
                                {
                                    FriendlyException.RaiseExeption("An error occurred while sending email", "Error", HttpStatusCode.InternalServerError);
                                }
                            }
                            else
                            {
                                FriendlyException.RaiseExeption("User don't have a valid email address. Please contact " + MvcApplication.HELP_DESK_EMAIL, "Error", HttpStatusCode.PreconditionFailed);
                            }
                            #endregion
                        }
                        contacts.Contact.Add(contact);
                    }
                    outgoing.Items.Add(contacts);
                }
            }
            return(Ok(outgoing));
        }
Пример #2
0
        public IHttpActionResult ForgotUserPassword([FromBody] NSALK.Models.UserProfile.SystemDataMessage incoming)
        {
            Console.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
            NSALK.Models.UserProfile.SystemDataResponse outgoing = new Models.UserProfile.SystemDataResponse();
            if (incoming != null)
            {
                int memberId = Convert.ToInt32(User.Identity.GetUserId());
                foreach (var cts in incoming.Contacts)
                {
                    NSALK.Models.UserProfile.SystemDataResponseContacts contacts = new Models.UserProfile.SystemDataResponseContacts();

                    foreach (var ct in cts.Contact)
                    {
                        NSALK.Models.UserProfile.SystemDataResponseContact contact = new Models.UserProfile.SystemDataResponseContact();
                        using (NSAKLEntities context = new NSAKLEntities())
                        {
                            #region Update Password for User
                            var user = context.members.Where(x => x.username == ct.Username).FirstOrDefault();

                            if (user == null)
                            {
                                FriendlyException.RaiseExeption("Login name does not exist", "Error", HttpStatusCode.PreconditionFailed);
                            }

                            if (MvcApplication.isValidEmail(user.email_address_default) || MvcApplication.isValidEmail(user.email_address_alternate))
                            {
                                string newPassword = "******";
                                newPassword = NSALK.MvcApplication.RandomPassword.Generate(6);
                                string hashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(newPassword, "sha1");

                                var usr = context.users.Where(x => x.username == ct.Username).FirstOrDefault();
                                usr.password = hashedPassword;


                                string errorMessage = "";
                                string email        = ct.Emailaddressdefault;
                                if (MvcApplication.isValidEmail(user.email_address_default))
                                {
                                    email = user.email_address_default;
                                }
                                else
                                if (MvcApplication.isValidEmail(user.email_address_alternate))
                                {
                                    email = user.email_address_alternate;
                                }


                                if (MvcApplication.SendEmail(email, "Your NSAKL password", "Your new NSAKL password is" + Environment.NewLine + newPassword, false, ref errorMessage))
                                {
                                    contact.Username = ct.Username;
                                    context.SaveChanges();
                                }
                                else
                                {
                                    FriendlyException.RaiseExeption("An error occurred while sending email", "Error", HttpStatusCode.InternalServerError);
                                }
                            }
                            else
                            {
                                FriendlyException.RaiseExeption("User don't have a valid email address. Please contact " + MvcApplication.HELP_DESK_EMAIL, "Error", HttpStatusCode.PreconditionFailed);
                            }
                            #endregion
                        }
                        contacts.Contact.Add(contact);
                    }
                    outgoing.Items.Add(contacts);
                }
            }
            return(Ok(outgoing));
        }