Exemplo n.º 1
0
        public ActionResult UserAdd(Users_Model users_, HttpPostedFileBase ImageString)
        {
            if (string.IsNullOrEmpty(users_.FullName) || string.IsNullOrEmpty(users_.Email) || users_.MobileNo <= 0 || users_.UserProfileID == null)
            {
                ViewBag.AddUserError = "Error";
                return(View("Users"));
            }
            else
            {
                if (Users_Interface_.checkEmail(users_.Email) == false)
                {
                    if (Users_Interface_.checkMobileNo(users_.MobileNo) == false)
                    {
                        var    Datas             = new Users_Model();
                        string GeneratedPassword = Users_Interface_.GeneratePassword();
                        string RandomNumber      = Users_Interface_.GenerateRandomNumber();
                        var    Salt         = "SHA1" + users_.Email + "SalesTrackingSystem";
                        var    UserPassword = Crypto.SHA1(Salt + GeneratedPassword);

                        string Root           = "~/UserInformation";
                        string Email          = users_.Email;
                        string FullName       = users_.FullName;
                        string RootDir        = Server.MapPath(Root);
                        string UserDirectory  = Server.MapPath(Root + "/" + Email);
                        string ImageDirectory = Server.MapPath(Root + "/" + Email + "/" + "Images");
                        string FileDirectory  = Server.MapPath(Root + "/" + Email + "/" + "Documents");
                        var    ImageName      = "";

                        if (users_.ImageString != null)
                        {
                            ImageName = RandomNumber + Path.GetExtension(ImageString.FileName).ToString();
                        }

                        Datas.DistrubitorID = users_.DistrubitorID;
                        Datas.UserProfileID = users_.UserProfileID;
                        Datas.FullName      = users_.FullName;
                        Datas.PasswordHash  = UserPassword;
                        Datas.Email         = users_.Email;
                        Datas.MobileNo      = users_.MobileNo;
                        Datas.UsersStatus   = users_.UsersStatus;
                        Datas.ImageString   = "/UserInformation/" + Email + "/" + "Images/" + ImageName;
                        if (Users_Interface_.SaveUserAccount(Datas))
                        {
                            if (!Directory.Exists(RootDir))
                            {
                                Directory.CreateDirectory(RootDir);
                            }

                            if (!Directory.Exists(UserDirectory))
                            {
                                Directory.CreateDirectory(UserDirectory);
                                if (Directory.Exists(UserDirectory))
                                {
                                    Directory.CreateDirectory(ImageDirectory);
                                    if (ImageString != null)
                                    {
                                        string imagePath = Path.Combine(Server.MapPath(Root + "/" + Email + "/" + "Images/" + ImageName));
                                        ImageString.SaveAs(imagePath);
                                    }
                                    Directory.CreateDirectory(FileDirectory);
                                }
                            }
                        }

                        string subject        = "Account Setup!";
                        string subjectTitle   = "Account Setup";
                        string userName       = FullName;
                        string message        = "Your account has been registered to our server. Please enter <b>" + GeneratedPassword + "</b> as your password on first Login.";
                        string warningMessage = "If this wasn't you please ignore this email. Verifying the email will only activate your account.";
                        string appLink        = "https://" + Request.ServerVariables["HTTP_HOST"];
                        string copyrightDate  = DateTime.Now.Year.ToString();
                        try
                        {
                            //Configuring webMail class to send emails
                            //gmail smtp server
                            WebMail.SmtpServer = "smtp.gmail.com";

                            //gmail port to send emails
                            WebMail.SmtpPort = 587;
                            WebMail.SmtpUseDefaultCredentials = true;

                            //sending emails with secure protocol
                            WebMail.EnableSsl = true;

                            //EmailId used to send emails from application
                            WebMail.UserName = "******";
                            WebMail.Password = "******";

                            //Sender email address.
                            WebMail.From = "*****@*****.**";

                            //Send email
                            WebMail.Send(to: Email, subject: subject, body: EmailBody(subjectTitle, subject, userName, message, warningMessage, appLink, copyrightDate), isBodyHtml: true);
                            Session["Success"] = "An account has been created and email has been sent to " + Email + ".";
                            return(RedirectToAction("Users"));
                        }
                        catch (Exception)
                        {
                            Session["Error"] = "Problem while sending email but account has been created.";
                            return(View("Users"));
                        }
                    }
                    else
                    {
                        ViewBag.AddUserError = "Error";
                        Session["Error"]     = users_.MobileNo + " exists please try different  mobile number!!";
                        return(View("Users"));
                    }
                }
                else
                {
                    ViewBag.AddUserError = "Error";
                    Session["Error"]     = users_.Email + "exists please try different email !!";
                    return(View("Users"));
                }
            }
        }
        public ActionResult Forget(string email)
        {
            var LoginSession = new Users_Model();

            if (Users.checkEmail(email))
            {
                LoginSession = Users.GetModelByEmail(email);
                string generatedToken = Users.GenerateRandomString(20, 80);
                if (Verification.updateResetAuthentication(LoginSession.UserID, DateTime.Now, generatedToken))
                {
                    string subject        = "Reset Password!";
                    string subjectTitle   = "reset password";
                    string userName       = LoginSession.FullName;
                    string message        = "Your request to reset password has been accepted. Please confirm it's you by clicking the link below. This Link is valid for 10 minuts only.";
                    string redirectUrl    = "https://" + Request.ServerVariables["HTTP_HOST"] + "/Auth/Reset?uac=" + email + "&uid=" + generatedToken;
                    string warningMessage = "If this wasn't you please ignore this email. Verifying the email will only allow to reset password.";
                    string appLink        = "https://" + Request.ServerVariables["HTTP_HOST"];
                    string copyrightDate  = DateTime.Now.Year.ToString();
                    try
                    {
                        //Configuring webMail class to send emails
                        //gmail smtp server
                        WebMail.SmtpServer = "smtp.gmail.com";

                        //gmail port to send emails
                        WebMail.SmtpPort = 587;
                        WebMail.SmtpUseDefaultCredentials = true;

                        //sending emails with secure protocol
                        WebMail.EnableSsl = true;

                        //EmailId used to send emails from application
                        WebMail.UserName = "******";
                        WebMail.Password = "******";

                        //Sender email address.
                        WebMail.From = "*****@*****.**";

                        //Send email
                        WebMail.Send(to: LoginSession.Email, subject: subject, body: EmailBody(subjectTitle, subject, userName, message, redirectUrl, warningMessage, appLink, copyrightDate), isBodyHtml: true);
                        Session["Success"] = "An email has been successfully to your account.";
                    }
                    catch (Exception)
                    {
                        Session["Error"] = "Problem while sending email.";
                    }
                    ViewBag.UserName = LoginSession.FullName;
                    return(View("VerificationEmail"));
                }
                else
                {
                    Session["Error"] = "Problem while sending email.";
                    return(View("ForgetPassword"));
                }
            }
            else
            {
                Session["Error"] = "The credentials you provide doesn't match our database.";
                return(View("ForgetPassword"));
            }
        }