예제 #1
0
        public ActionResult ForgotPassword(ForgotPassword model, string ReturnUrl = "")
        {
            string message = "";
            bool   Status  = false;
            string type    = "Success";

            using (MyDatabaseEntities dc = new MyDatabaseEntities())
            {
                var user = dc.Users.Where(x => x.EmailID == model.EmailID).FirstOrDefault();
                if (user != null)
                {
                    string psw = Encryption.CustomEnrypt.GeneratePassword(10);
                    var    usr = new UserController();
                    while (!usr.checkPasswordStrengt(psw))
                    {
                        psw = Encryption.CustomEnrypt.GeneratePassword(10);
                    }
                    string NewkeyOne  = CustomEnrypt.RandomString(15);
                    string NewkeyTwo  = CustomEnrypt.RandomString(15);
                    string NewkeyTree = CustomEnrypt.RandomString(15);
                    user.Password = CustomEnrypt.Encrypt(psw, NewkeyOne);
                    //user.Password = Crypto.Hash(user.Password);
                    user.ConfirmPassword = CustomEnrypt.Encrypt(psw, NewkeyOne);
                    user.TempPasswordSet = true;
                    var crypto = dc.cryptokeys.Where(x => x.UserID == user.UserID).FirstOrDefault();
                    crypto.cryptone  = CustomEnrypt.Encrypt(NewkeyOne, NewkeyTwo);
                    crypto.crypttwo  = CustomEnrypt.Encrypt(NewkeyTwo, NewkeyTree);
                    crypto.crypttree = NewkeyTree;
                    try
                    {
                        dc.SaveChanges();
                        SendTempPassword(user, psw);
                        message = "temperary password send to given mail";
                        type    = "Success";
                        Status  = true;
                    }
                    catch (DbEntityValidationException ex)
                    {
                        foreach (var entityValidationErrors in ex.EntityValidationErrors)
                        {
                            foreach (var validationError in entityValidationErrors.ValidationErrors)
                            {
                                Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
                            }
                        }
                    }
                }
                else
                {
                    message = "there are no user matching the given email address please contact administartor if error continues";
                    type    = "Error";
                    Status  = true;
                }
            }

            ViewBag.Message = message;
            ViewBag.Status  = Status;
            ViewBag.Type    = type;
            return(View());
        }
예제 #2
0
        public ActionResult ChangePassword(ChangePassword model, string ReturnUrl = "")
        {
            string message     = "";
            bool   Status      = false;
            string type        = "Success";
            var    userControl = new UserController();

            if (!userControl.checkPasswordStrengt(model.NewPassword))
            {
                message = "password does not match rules please include one uppercase letter, one number and one of these symbols !,@,#,$,%,^,&,*,?,_,~,-,£,(,)";
                Status  = true;
                type    = "Error";
            }
            else
            {
                var CurrentUserEmail = HttpContext.User.Identity.Name;
                using (MyDatabaseEntities dc = new MyDatabaseEntities())
                {
                    var user                  = dc.Users.Where(x => x.EmailID == CurrentUserEmail).FirstOrDefault();
                    var getPassword           = dc.Users.Where(u => u.EmailID == user.EmailID).Select(u => u.Password);
                    var materializePassword   = getPassword.ToList();
                    var password              = materializePassword[0];
                    var getCryptoOne          = dc.cryptokeys.Where(u => u.UserID == user.UserID).Select(u => u.cryptone);
                    var materializeCryptoOne  = getCryptoOne.ToList();
                    var CryptoOne             = materializeCryptoOne[0];
                    var getCryptoTwo          = dc.cryptokeys.Where(u => u.UserID == user.UserID).Select(u => u.crypttwo);
                    var materializeCryptoTwo  = getCryptoTwo.ToList();
                    var CryptoTwo             = materializeCryptoTwo[0];
                    var getCryptoTree         = dc.cryptokeys.Where(u => u.UserID == user.UserID).Select(u => u.crypttree);
                    var materializeCryptoTree = getCryptoTree.ToList();
                    var keyTree               = materializeCryptoTree[0];
                    var keyTwo                = CustomDecrypt.Decrypt(CryptoTwo, keyTree);
                    var keyOne                = CustomDecrypt.Decrypt(CryptoOne, keyTwo);
                    var decryptPassword       = CustomDecrypt.Decrypt(password, keyOne);

                    if ((string.Compare(model.Password, decryptPassword) == 0))
                    {
                        string NewkeyOne  = CustomEnrypt.RandomString(15);
                        string NewkeyTwo  = CustomEnrypt.RandomString(15);
                        string NewkeyTree = CustomEnrypt.RandomString(15);
                        user.Password = CustomEnrypt.Encrypt(model.NewPassword, NewkeyOne);
                        //user.Password = Crypto.Hash(user.Password);
                        user.ConfirmPassword = CustomEnrypt.Encrypt(model.ConfirmPassword, NewkeyOne);
                        var crypto = dc.cryptokeys.Where(x => x.UserID == user.UserID).FirstOrDefault();
                        crypto.cryptone  = CustomEnrypt.Encrypt(NewkeyOne, NewkeyTwo);
                        crypto.crypttwo  = CustomEnrypt.Encrypt(NewkeyTwo, NewkeyTree);
                        crypto.crypttree = NewkeyTree;
                        try
                        {
                            if (user.TempPasswordSet)
                            {
                                user.TempPasswordSet = false;
                                user.Failed_Logins   = 0;
                            }
                            dc.SaveChanges();
                            message = "Your password have been successfuly changed.";
                            Status  = true;
                        }
                        catch (DbEntityValidationException ex)
                        {
                            foreach (var entityValidationErrors in ex.EntityValidationErrors)
                            {
                                foreach (var validationError in entityValidationErrors.ValidationErrors)
                                {
                                    Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
                                }
                            }
                        }
                    }
                    else
                    {
                        message = "Password does not match if Error continues contact administrator.";
                        Status  = true;
                        type    = "Error";
                    }
                }
            }

            ViewBag.Message = message;
            ViewBag.Status  = Status;
            ViewBag.Type    = type;
            return(View());
        }
        public ActionResult Registration([Bind(Exclude = "IsEmailVerified,ActivationCode")] User user)
        {
            bool   Status  = false;
            string message = "";

            //
            // Model Validation
            if (ModelState.IsValid)
            {
                #region //Email is already Exist
                var isExist = IsEmailExist(user.EmailID);
                if (isExist)
                {
                    ModelState.AddModelError("EmailExist", "Email already exist");
                    return(View(user));
                }
                if (!checkPasswordStrengt(user.Password))
                {
                    message = "password does not match rules please include one uppercase letter, one number and one of these symbols !,@,#,$,%,^,&,*,?,_,~,-,£,(,)";
                    Status  = true;
                }
                else
                {
                    #endregion

                    #region Generate Activation Code
                    user.ActivationCode = Guid.NewGuid();
                    #endregion

                    #region  Password Hashing
                    string keyOne  = CustomEnrypt.RandomString(15);
                    string keyTwo  = CustomEnrypt.RandomString(15);
                    string keyTree = CustomEnrypt.RandomString(15);
                    user.Password = CustomEnrypt.Encrypt(user.Password, keyOne);
                    //user.Password = Crypto.Hash(user.Password);
                    user.ConfirmPassword = CustomEnrypt.Encrypt(user.ConfirmPassword, keyOne);
                    #endregion
                    user.IsEmailVerified = false;

                    #region Save to Database
                    using (MyDatabaseEntities dc = new MyDatabaseEntities())
                    {
                        User new_user = dc.Users.Create();
                        new_user = user;
                        cryptokey crypto = dc.cryptokeys.Create();
                        crypto.cryptone  = CustomEnrypt.Encrypt(keyOne, keyTwo);
                        crypto.crypttwo  = CustomEnrypt.Encrypt(keyTwo, keyTree);
                        crypto.crypttree = keyTree;
                        dc.Users.Add(new_user);
                        dc.cryptokeys.Add(crypto);
                        try
                        {
                            dc.SaveChanges();
                        }
                        catch (DbEntityValidationException ex)
                        {
                            foreach (var entityValidationErrors in ex.EntityValidationErrors)
                            {
                                foreach (var validationError in entityValidationErrors.ValidationErrors)
                                {
                                    Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
                                }
                            }
                        }

                        //Send Email to User
                        SendVerificationLinkEmail(user.EmailID, user.ActivationCode.ToString());
                        message = "Registration successfully done. Account activation link " +
                                  " has been sent to your email id:" + user.EmailID;
                        Status = true;
                    }
                    #endregion
                }
            }
            else
            {
                message = "Invalid Request";
            }

            ViewBag.Message = message;
            ViewBag.Status  = Status;
            return(View(user));
        }