public bool IsEmailExist(string emailID)
 {
     using (DNDAuthDBContext dc = new DNDAuthDBContext())
     {
         var v = dc.Users.Where(a => a.EmailID == emailID).FirstOrDefault();
         return(v != null);
     }
 }
        //public IEnumerable<SecurityQuestions> SecurityQuestionList()
        //{
        //    using (DNDAuthDBContext dc = new DNDAuthDBContext())
        //    {
        //        var result = dc.SecurityQuestions.ToList();

        //        return result;
        //    }

        //}

        //[NonAction]
        //public SelectList ToSelectList(IEnumerable<SecurityQuestions> table, string valueField, string textField)
        //{
        //    List<SelectListItem> list = new List<SelectListItem>();

        //    foreach (SecurityQuestions row in table)
        //    {
        //        list.Add(new SelectListItem()
        //        {
        //            Text = row.SecurityQues.ToString(),
        //            Value = row.SecurityQuesID.ToString()
        //        });
        //    }

        //    return new SelectList(list, "Value", "Text");
        //}

        public static SelectList LoadAllSecurityQuestions()
        {
            using (DNDAuthDBContext dc = new DNDAuthDBContext())
            {
                var result = dc.SecurityQuestions.ToList();
                var items  = result.Select(x => new { x.SecurityQuesID, x.SecurityQues }).ToList();
                items.Insert(0, new { SecurityQuesID = 0, SecurityQues = "---- Select ----" });
                return(new SelectList(items, "SecurityQuesID", "SecurityQues"));
            }
        }
        public async Task <IActionResult> Login(UserLogin login)
        {
            try
            {
                string message = "";
                using (DNDAuthDBContext dc = new DNDAuthDBContext())
                {
                    var v = dc.Users.Where(a => a.EmailID == login.EmailID).FirstOrDefault();
                    if (v != null)
                    {
                        if (!v.IsEmailVerified)
                        {
                            ViewBag.Message = "Please verify your email first";
                            return(View());
                        }

                        if (string.Compare(Crypto.Hash(login.Password), v.Password) == 0)
                        {
                            var claims = new List <Claim>
                            {
                                new Claim(ClaimTypes.Name, v.EmailID)
                            };

                            ClaimsIdentity  userIdentity = new ClaimsIdentity(claims, "login");
                            ClaimsPrincipal principal    = new ClaimsPrincipal(userIdentity);

                            await HttpContext.SignInAsync(principal);

                            HttpContext.Session.SetString("EmailID", v.EmailID.ToString());

                            return(RedirectToAction("Index", "Home"));
                        }
                        else
                        {
                            message = "Invalid credential provided";
                        }
                    }
                    else
                    {
                        message = "Invalid credential provided";
                    }
                }
                ViewBag.Message = message;
                return(View());
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message.ToString();
                return(View());
            }
        }
        public ActionResult PasswordRecovery(User usr)
        {
            bool   Status  = false;
            string message = "";

            var SessionVal = HttpContext.Session.GetString("EmailID");

            if (string.IsNullOrEmpty(SessionVal))
            {
                return(RedirectToAction("Login", "User"));
            }

            string SecurityAns = "";

            using (DNDAuthDBContext dc = new DNDAuthDBContext())
            {
                SecurityAns = dc.Users.Where(x => x.EmailID == SessionVal).First().SecurityAnswer;
            }

            if (string.Compare(usr.SecurityAnswer, SecurityAns) == 0)
            {
                Status = true;
                return(RedirectToAction("ChangePassword", "User"));
            }
            else
            {
                message         = "Answer is Incorrect. Try Again.";
                ViewBag.Message = message;
                ViewBag.Status  = Status;

                string SecQues = HttpContext.Session.GetString("SecQues");

                ViewBag.SecurityQues = SecQues;

                //return RedirectToAction("PasswordRecovery", "User");
            }

            //ViewBag.SecurityQuestionList = ToSelectList(SecurityQuestionList(), "ID", "SecurityQues");
            return(View());
        }
        public ActionResult ChangePassword(User usr)
        {
            var SessionVal = HttpContext.Session.GetString("EmailID");

            if (string.IsNullOrEmpty(SessionVal))
            {
                return(RedirectToAction("Login", "User"));
            }

            using (DNDAuthDBContext dc = new DNDAuthDBContext())
            {
                var RealUserData = dc.Users.Where(x => x.EmailID == SessionVal).FirstOrDefault();

                RealUserData.Password        = Crypto.Hash(usr.Password);
                RealUserData.ConfirmPassword = Crypto.Hash(usr.ConfirmPassword);

                dc.Users.Update(RealUserData);
                dc.SaveChanges();
            }

            return(View());
        }
        public ActionResult PasswordRecovery()
        {
            var SessionVal = HttpContext.Session.GetString("EmailID");

            if (string.IsNullOrEmpty(SessionVal))
            {
                return(RedirectToAction("Login", "User"));
            }

            string SecQues = "";

            using (DNDAuthDBContext dc = new DNDAuthDBContext())
            {
                var SecQID = dc.Users.Where(x => x.EmailID == SessionVal).First().SecurityQuesID;
                SecQues = dc.SecurityQuestions.Where(x => x.SecurityQuesID == SecQID).First().SecurityQues;
            }

            ViewBag.SecurityQues = SecQues;

            HttpContext.Session.SetString("SecQues", SecQues);

            return(View());
        }
        public ActionResult VerifyAccount(string id)
        {
            bool Status = false;

            using (DNDAuthDBContext dc = new DNDAuthDBContext())
            {
                //dc.Configuration.ValidateOnSaveEnabled = false; // This line I have added here to avoid
                // Confirm password does not match issue on save changes
                var v = dc.Users.Where(a => a.ActivationCode == new Guid(id)).FirstOrDefault();
                if (v != null)
                {
                    v.IsEmailVerified = true;
                    dc.SaveChanges();
                    Status = true;
                }
                else
                {
                    ViewBag.Message = "Invalid Request";
                }
            }
            ViewBag.Status = Status;
            return(View());
        }
        public ActionResult Registration(User user)
        {
            using (DNDAuthDBContext dc = new DNDAuthDBContext())
            {
                bool   Status  = false;
                string message = "";
                ViewBag.SecurityQuesID = LoadAllSecurityQuestions();
                //ViewBag.SecurityQuestionList = ToSelectList(SecurityQuestionList(), "SecurityQuesID", "SecurityQues");

                using (var transaction = dc.Database.BeginTransaction())
                {
                    try
                    {
                        //
                        // 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));
                            }
                            #endregion

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

                            #region  Password Hashing
                            user.Password        = Crypto.Hash(user.Password);
                            user.ConfirmPassword = Crypto.Hash(user.ConfirmPassword); //
                            #endregion
                            user.IsEmailVerified = false;

                            #region Save to Database

                            dc.Users.Add(user);
                            dc.SaveChanges();

                            //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;

                            transaction.Commit();

                            #endregion
                        }
                        else
                        {
                            message = "Invalid Request";
                        }

                        ViewBag.Message = message;
                        ViewBag.Status  = Status;
                        return(View(user));
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();

                        string Error = ex.Message.ToString();
                        ViewBag.Message = Error;
                        ViewBag.Status  = Status;

                        return(View(user));
                    }
                }
            }
        }