Exemplo n.º 1
0
        public ActionResult ForgotPassword(ManageUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                ServiceLayer.Services.ResetPasswordService _ResetPasswordService = new ServiceLayer.Services.ResetPasswordService();
                var    token    = "";
                string UserName = WebSecurity.CurrentUserName;
                //check user existance


                var user = Membership.GetUser(UserName);

                bool changePasswordSucceeded;
                changePasswordSucceeded = user.ChangePassword(model.OldPassword, model.NewPassword);

                if (!changePasswordSucceeded)
                {
                    return(Content("Current password is not correct."));
                }

                if (user == null)
                {
                    TempData["Message"] = "User Not exist.";
                }
                else
                {
                    //generate password token
                    token = WebSecurity.GeneratePasswordResetToken(UserName);
                    //create url with above token
                }
                bool any      = _ResetPasswordService.UpdatePassword(UserName, token);
                bool response = false;
                if (any == true)
                {
                    response = WebSecurity.ResetPassword(token, model.NewPassword);
                    if (response == true)
                    {
                        try
                        {
                            //  Here Maintain Password History
                            //  MembershipUser u = Membership.GetUser(WebSecurity.CurrentUserName, false);

                            string RetPassword = HashData(model.NewPassword);
                            SecUserPasswordHistory _secUserPasswordHistory = new SecUserPasswordHistory();
                            byte[] array = Encoding.ASCII.GetBytes(RetPassword);

                            _secUserPasswordHistory.PasswordHash256 = array;
                            _secUserPasswordHistory.DeleteFlag      = false;
                            _secUserPasswordHistory.RowVersion      = null;
                            _secUserPasswordHistory.SecUserID       = (WebSecurity.CurrentUserId);
                            _ResetPasswordService.AddPasswordHistory(_secUserPasswordHistory);
                            TempData["Message"] = "Password changed.";
                        }
                        catch (Exception ex)
                        {
                            TempData["Message"] = "Error occured while changing Password." + ex.Message;
                        }
                    }
                    else
                    {
                        TempData["Message"] = "Hey, avoid random request on this page.";
                    }
                }
                else
                {
                    TempData["Message"] = "Username and token not maching.";
                }
            }
            return(View(model));
        }
Exemplo n.º 2
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            ServiceLayer.Services.ScreenPermissionService _ActionAccessPermissionService = new ServiceLayer.Services.ScreenPermissionService();

            #region Commented Code

            //if (ModelState.IsValid)
            //{
            //    var user = await UserManager.FindAsync(model.UserName, model.Password);
            //    if (user != null)
            //    {
            //        await SignInAsync(user, model.RememberMe);
            //        return RedirectToLocal(returnUrl);
            //    }
            //    else
            //    {
            //        ModelState.AddModelError("", "Invalid username or password.");
            //    }
            //}

            #endregion

            //// If we got this far, something failed, redisplay form
            //return View(model);
            if (ModelState.IsValid)
            {
                ServiceLayer.Services.ResetPasswordService _ResetPasswordService = new ServiceLayer.Services.ResetPasswordService();
                List <User> list = _ResetPasswordService.GetUsersByEmail(model.UserName.ToString());
                if (list.Count > 0)
                {
                    if (list[0].Active == true)
                    {
                        if (WebSecurity.Login(model.UserName, model.Password))
                        {
                            //if (list[0].Active == true)
                            //{
                            int cID      = WebSecurity.GetUserId(model.UserName);
                            var username = WebSecurity.CurrentUserName;
                            // session["userid"] = cid;convert.toint32(membership.getuser().provideruserkey
                            string TokenID = _ActionAccessPermissionService.GetAuthorizeToken(Convert.ToInt32(cID));
                            Session["TokenID"] = TokenID;
                            if (Session["TokenID"] == "")
                            {
                                TokenID            = _ActionAccessPermissionService.GetAuthorizeToken(Convert.ToInt32(cID));
                                Session["TokenID"] = TokenID;
                            }
                            if (returnUrl != null && returnUrl != "/")
                            {
                                return(Redirect(returnUrl));
                            }
                            return(RedirectToAction("Index", "Home"));
                            //}
                            //else
                            //{
                            //    return RedirectToAction("AccountInActive", "Home");
                            //}
                        }
                        else
                        {
                            ModelState.AddModelError("", "Sorry,Username or Password is Invalid.");
                            return(View(model));
                        }
                    }
                    else
                    {
                        return(RedirectToAction("AccountInActive", "Account"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Sorry,Username or Password is Invalid.");
                    return(View(model));
                }
            }
            ModelState.AddModelError("", "Sorry,Username or Password is Invalid.");
            return(View(model));
        }
Exemplo n.º 3
0
        public ActionResult Register(RegisterViewModel model)
        {
            //Validating Captcha
            string stringResponse = string.Empty;

            if (!ValidateCaptcha(out stringResponse))
            {
                ModelState.AddModelError("", stringResponse);
                //Below code regarding Invalid captcha has been commented as currently we dont have any secret key for this application
                //return View(model);
            }
            ServiceLayer.Services.ResetPasswordService _ResetPasswordService = new ServiceLayer.Services.ResetPasswordService();

            //if (ModelState.IsValid)
            {
                #region Commented Code

                //var user = new ApplicationUser() { UserName = model.UserName };
                //var result = await UserManager.CreateAsync(user, model.Password);
                //if (result.Succeeded)
                //{
                //    await SignInAsync(user, isPersistent: false);
                //    return RedirectToAction("Index", "Home");
                //}
                //else
                //{
                //    AddErrors(result);
                //}
                #endregion

                try
                {
                    List <User> list    = _ResetPasswordService.GetUsersByEmail(model.UserName.ToString());
                    int         _userID = WebSecurity.GetUserId(model.UserName);
                    if (list.Count == 0 && _userID > 0)
                    {
                        ((SimpleMembershipProvider)Membership.Provider).DeleteUser(model.UserName.ToString(), true); // deletes record from webpages_Membership table
                    }

                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Active = false });
                    //TODO This Code Use For Mainain Password History
                    string RetPassword = HashData(model.Password);
                    SecUserPasswordHistory _secUserPasswordHistory = new SecUserPasswordHistory();
                    byte[] array = Encoding.ASCII.GetBytes(RetPassword);

                    userService.UpdateUserInfo(model.UserName);
                    _secUserPasswordHistory.PasswordHash256 = array;
                    _secUserPasswordHistory.DeleteFlag      = false;
                    _secUserPasswordHistory.RowVersion      = null;
                    _secUserPasswordHistory.SecUserID       = WebSecurity.GetUserId(model.UserName);
                    _ResetPasswordService.AddPasswordHistory(_secUserPasswordHistory);
                    //End
                    //  ModelState.AddModelError("", "User has been successfully created..");
                    return(RedirectToAction("Index", "Home"));
                    //  return null;
                }
                catch (Exception ex)
                {
                    // ModelState.AddModelError("", "User already exist..");
                }
            }
            ModelState.AddModelError("", "User already exist..");
            // If we got this far, something failed, redisplay form
            return(View(model));
        }