public bool IsEmailExist(string email)
 {
     using (MidasEMSEntities dc = new MidasEMSEntities())
     {
         var v = dc.Users.Where(a => a.Email == email).FirstOrDefault();
         return(v != null);
     }
 }
        public ActionResult Login(UserLogin login, string ReturnUrl)
        {
            string message = "";

            using (MidasEMSEntities dc = new MidasEMSEntities())
            {
                var v = dc.Users.Where(a => a.Email == login.Email).FirstOrDefault();
                if (v != null)
                {
                    if (string.Compare(Crypto.Hash(login.Password), v.Password) == 0)
                    {
                        int    timeout   = login.RememberMe ? 525600 : 20; // 525600 min = 1 year
                        var    ticket    = new FormsAuthenticationTicket(login.Email, login.RememberMe, timeout);
                        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);


                        if (Url.IsLocalUrl(ReturnUrl))
                        {
                            return(Redirect(ReturnUrl));
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                    else
                    {
                        message = "Invalid Credentials Provided";
                    }
                }
                else
                {
                    message = "Invalid Credentials Provided";
                }
            }

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

            using (MidasEMSEntities dc = new MidasEMSEntities())
            {
                dc.Configuration.ValidateOnSaveEnabled = false; // Avoid Confirm Password Match
                var v = dc.Users.Where(a => a.ActivationCode == new Guid(id)).FirstOrDefault();
                if (v != null)
                {
                    v.IsEmailVerified = true;
                    dc.SaveChanges();
                    Status = true;
                }
                else
                {
                    ViewBag.Status("Invalid Request");
                }
            }
            ViewBag.Status = Status;
            return(View());
        }
        public ActionResult Registration([Bind(Exclude = "IsEmailVerified,ActivationCode")] User user)
        {
            bool   Status  = false;
            string message = "";

            //Model Validation
            if (ModelState.IsValid)
            {
                #endregion

                #region Email Already Exists
                var isExist = IsEmailExist(user.Email);
                if (isExist)
                {
                    ModelState.AddModelError("EmailExist", "Email already exist");
                    return(View());
                }
                #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

                #region SetIsEmailVerifiedFalse

                user.IsEmailVerified = false;

                #endregion

                #region Save Data To DB

                using (MidasEMSEntities dc = new MidasEMSEntities())
                {
                    dc.Users.Add(user);
                    dc.SaveChanges();

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

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