public ActionResult ChangePassword(ManageUserViewModel model)
        {
            var message = "";
            DataCollectionModelDataContext db = new DataCollectionModelDataContext();
            var RankUser = db.RankUsers.Where(a => a.UserID == SessionManager.UserId.Trim()).FirstOrDefault();

            if (RankUser != null)
            {
                if (string.Compare(FormCommonMethods.Encryptdata(model.OldPassword), RankUser.UserPassword) == 0)
                {
                    RankUser.UserPassword = FormCommonMethods.Encryptdata(model.NewPassword);
                    db.SubmitChanges();
                    message = "Password updated successfully";
                    //ViewBag.Status = true;
                    //ViewBag.Message = message;
                    TempData["Message"] = message;
                    TempData["Status"]  = true;
                    return(Redirect(SessionManager.MenuList.FirstOrDefault().MenuUrl));
                }
                else
                {
                    message        = "Invalid Old Password";
                    ViewBag.Status = false;
                }
            }
            ViewBag.Message = message;
            return(View(model));
        }
        public ActionResult ResetPassword(ResetPasswordModel model)
        {
            var message = "";

            if (ModelState.IsValid)
            {
                using (DataCollectionModelDataContext db = new DataCollectionModelDataContext())
                {
                    var user = db.RankUsers.Where(a => a.ResetPasswordCode == model.ResetCode).FirstOrDefault();
                    if (user != null)
                    {
                        user.UserPassword      = FormCommonMethods.Encryptdata(model.NewPassword);
                        user.ResetPasswordCode = "";
                        db.SubmitChanges();
                        message = "New password updated successfully";
                        //ViewBag.Status = true;
                        //ViewBag.Message = message;
                        TempData["Message"] = message;
                        TempData["Status"]  = true;
                        return(RedirectToAction("Login", "User", null));
                    }
                }
            }
            else
            {
                message        = "invalid details. Contact System Administrator.";
                ViewBag.Status = false;
            }
            ViewBag.Message = message;
            return(View(model));
        }
        //[ValidateAntiForgeryToken]
        public ActionResult Login(LoginViewModel loginViewModel, string ReturnUrl = "")
        {
            string message = "";

            using (DataCollectionModelDataContext db = new DataCollectionModelDataContext())
            {
                DataCollection.Models.RankUser RankUser = db.RankUsers.Where(a => a.UserID == loginViewModel.UserId.Trim()).FirstOrDefault();
                if (RankUser != null)
                {
                    if (string.Compare(FormCommonMethods.Encryptdata(loginViewModel.Password), RankUser.UserPassword) == 0)
                    {
                        if (string.IsNullOrWhiteSpace(Convert.ToString(RankUser.UserValid)) || Convert.ToString(RankUser.UserValid).ToUpper().Trim() == "N" || (string.IsNullOrWhiteSpace(RankUser.DeptID) || string.IsNullOrWhiteSpace(RankUser.UserRole) || string.IsNullOrWhiteSpace(RankUser.UserWork)))
                        {
                            if (Convert.ToString(RankUser.UserValid).ToUpper().Trim() == "N" && RankUser.UserDisabledOn != null)
                            {
                                ViewBag.Message = "Your account is blocked. Please Contact Admin";
                                ViewBag.Status  = false;
                            }
                            else if (Convert.ToString(RankUser.UserValid).ToUpper().Trim() == "N" && (!RankUser.IsEmailVerified ?? !false))
                            {
                                ViewBag.Message = "Email link sent. kindly click on the link to complete registration";
                                ViewBag.Status  = false;
                            }
                            else if (Convert.ToString(RankUser.UserValid).ToUpper().Trim() == "N" && (RankUser.IsEmailVerified ?? false))
                            {
                                string href = Url.Action("Index", "Contact", null);
                                var    link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, href);
                                ViewBag.Message = "Activation pending from Admin , IRD-SRIC. Kindly wait for some more time or Contact through <a href='" + link + "'> Contact Us </a>";
                                ViewBag.Status  = false;
                            }
                            return(View());
                        }

                        int timeout = loginViewModel.RememberMe ? 525600 : 20; // 525600 min = 1 year
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, RankUser.UserName, DateTime.Now, DateTime.Now.AddMinutes(2880), loginViewModel.RememberMe, RankUser.UserRole, FormsAuthentication.FormsCookiePath);
                        string encrypted = FormsAuthentication.Encrypt(ticket);
                        var    cookie    = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
                        cookie.Expires  = DateTime.Now.AddMinutes(timeout);
                        cookie.HttpOnly = true;
                        Response.Cookies.Add(cookie);
                        SetUserSession(RankUser);
                        ViewBag.Status = true;
                        return(Redirect(SessionManager.MenuList.FirstOrDefault().MenuUrl));
                    }
                    else
                    {
                        message        = "Invalid credential provided";
                        ViewBag.Status = false;
                    }
                }
                else
                {
                    message        = "Invalid credential provided";
                    ViewBag.Status = false;
                }
            }

            ViewBag.Message = message;
            return(View());
        }
Exemple #4
0
        public ActionResult Login(LoginViewModel loginViewModel, string ReturnUrl = "")
        {
            string message = "";

            using (DataCollectionModelDataContext db = new DataCollectionModelDataContext())
            {
                DataCollection.Models.RankUser RankUser = db.RankUsers.Where(a => a.UserID == loginViewModel.UserId.Trim()).FirstOrDefault();
                if (RankUser != null)
                {
                    if (string.Compare(FormCommonMethods.Encryptdata(loginViewModel.Password), RankUser.UserPassword) == 0)
                    {
                        if (string.IsNullOrWhiteSpace(Convert.ToString(RankUser.UserValid)) || Convert.ToString(RankUser.UserValid).ToUpper().Trim() == "N" || (string.IsNullOrWhiteSpace(RankUser.DeptID) || string.IsNullOrWhiteSpace(RankUser.UserRole) || string.IsNullOrWhiteSpace(RankUser.UserWork)))
                        {
                            if (Convert.ToString(RankUser.UserValid).ToUpper().Trim() == "N" && RankUser.UserDisabledOn != null)
                            {
                                ViewBag.Message = "Your account is blocked. Please Contact Admin";
                            }
                            else
                            {
                                ViewBag.Message = "Your registration is under process";
                            }
                            return(View());
                        }

                        int timeout = loginViewModel.RememberMe ? 525600 : 20; // 525600 min = 1 year
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, RankUser.UserName, DateTime.Now, DateTime.Now.AddMinutes(2880), loginViewModel.RememberMe, RankUser.UserRole, FormsAuthentication.FormsCookiePath);
                        string encrypted = FormsAuthentication.Encrypt(ticket);
                        var    cookie    = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
                        cookie.Expires  = DateTime.Now.AddMinutes(timeout);
                        cookie.HttpOnly = true;
                        Response.Cookies.Add(cookie);
                        SetUserSession(RankUser);
                        ViewBag.Status = true;
                        return(Redirect(SessionManager.MenuList.FirstOrDefault().MenuUrl));
                    }
                    else
                    {
                        message        = "Invalid credential provided";
                        ViewBag.Status = false;
                    }
                }
                else
                {
                    message        = "Invalid credential provided";
                    ViewBag.Status = false;
                }
            }

            ViewBag.Message = message;
            return(View());
        }
        public ActionResult Registration(RegisterViewModel user)
        {
            bool   Status  = true;
            string message = "";

            // Model Validation
            if (!this.IsCaptchaValid("Captcha is not valid"))
            {
                Status  = false;
                message = "Captcha is not valid";
            }

            if (Status && ModelState.IsValid)
            {
                DataAccess.Entity.RankUser rankUser = new DataAccess.Entity.RankUser();

                #region  Password Hashing
                rankUser.UserPassword = FormCommonMethods.Encryptdata(user.UserPassword);
                #endregion
                rankUser.UserID          = user.UserId;
                rankUser.UserEmail       = user.UserEmail;
                rankUser.UserEmpNo       = user.UserEmpNo;
                rankUser.UserName        = user.UserName;
                rankUser.UserMob         = user.MobileNo;
                rankUser.DeptID          = user.DeptID;
                rankUser.UserWork        = user.UserWork;
                rankUser.UserRemarks     = user.UserRemarks;
                rankUser.UserValid       = "N";
                rankUser.UserRole        = UserRoles.User.ToString();
                rankUser.IsEmailVerified = false;
                rankUser.ActivationCode  = Guid.NewGuid();

                #region //Email is already Exist
                int isExist = IsEmailExist(rankUser);
                if (isExist == 1)
                {
                    ModelState.AddModelError("EmailExist", "Email already exist");
                    message = "Email already exist";
                    Status  = false;
                    //return View(user);
                }
                else if (isExist == 2)
                {
                    ModelState.AddModelError("EmpNo Exist", "EmpNo already exist");
                    message = "EmpNo already exist";
                    Status  = false;
                    //return View(user);
                }
                else if (isExist == 3)
                {
                    ModelState.AddModelError("UserId Exist", "UserId is taken");
                    message = "UserId is taken";
                    Status  = false;
                    //return View(user);
                }
                else if (isExist == 4)
                {
                    ModelState.AddModelError("Mobile Exist", "Mobile already exist");
                    message = "Mobile already exist";
                    Status  = false;
                    //return View(user);
                }

                #endregion

                if (Status)
                {
                    #region Save to Database
                    RankUserRepository rankUserRepository = new RankUserRepository();
                    Status = rankUserRepository.AddRankUser(rankUser);
                    if (Status)
                    {
                        //Send Email to User
                        SendVerificationLinkEmail(user.UserEmail, rankUser.ActivationCode.ToString());

                        user = new RegisterViewModel();
                        user = LoadRegister();
                        ModelState.Clear();
                        message = "Your account has been registered. An Email has been sent to confirm user registration." +
                                  "Kindly check & click on the link given in Email to register your details.";
                    }
                    else
                    {
                        message = "Your registration failed.";
                    }
                    #endregion
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(message))
                {
                    message = "Invalid Request";
                }
            }

            ViewBag.Message = message;
            ViewBag.Status  = Status;
            if (!Status)
            {
                RegisterViewModel RegisterVM = new RegisterViewModel();
                RegisterVM           = LoadRegister();
                user.UserWorkDDLList = RegisterVM.UserWorkDDLList;
                user.DeptDDLList     = RegisterVM.DeptDDLList;
            }
            return(View(user));
        }