Ejemplo n.º 1
0
        public ActionResult VerifyAccount(string id)
        {
            bool status = false;

            using (JITContext db = new JITContext())
            {
                db.Configuration.ValidateOnSaveEnabled = false;
                try
                {
                    var v = db.Users.Where(user => user.ActivationCode == new Guid(id)).FirstOrDefault();
                    if (v != null)
                    {
                        v.IsEmailVerified = true;
                        db.SaveChanges();
                        status = true;
                    }
                    else
                    {
                        ViewBag.Message = "Invalid Request";
                        ViewBag.Status  = false;
                    }
                }
                catch (FormatException e)
                {
                    ViewBag.Message = "Invalid Request";
                    ViewBag.Status  = false;
                }
            }
            ViewBag.Status = status;
            return(View());
        }
Ejemplo n.º 2
0
 public ActionResult Login(UserLogin login, string returnUrl = "")
 {
     using (JITContext db = new JITContext())
     {
         var v = db.Users.Where(user => user.UserName == login.UserName).FirstOrDefault();
         if (v != null)
         {
             if (System.Web.Helpers.Crypto.VerifyHashedPassword(v.Password, login.Password))
             {
                 int    timeout   = 525600;
                 var    ticket    = new FormsAuthenticationTicket(login.UserName, login.RememberMe, timeout);
                 string encrypted = FormsAuthentication.Encrypt(ticket);
                 var    cookie    = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
                 if (login.RememberMe)
                 {
                     cookie.Expires = DateTime.Now.AddHours(24);
                 }
                 cookie.HttpOnly = false;
                 Response.Cookies.Add(cookie);
                 if (Url.IsLocalUrl(returnUrl))
                 {
                     return(Redirect(returnUrl));
                 }
                 {
                     return(RedirectToAction("Index", "Home"));
                 }
             }
         }
     }
     ModelState.AddModelError("InvalidCredentials", "Wrong user name or password");
     return(View());
 }
Ejemplo n.º 3
0
 public bool UserNameExist(string userName)
 {
     using (JITContext db = new JITContext())
     {
         var v = db.Users.Where(user => user.UserName == userName).FirstOrDefault();
         return(v != null);
     }
 }
Ejemplo n.º 4
0
 public bool EmailExist(string email)
 {
     using (JITContext db = new JITContext())
     {
         var v = db.Users.Where(user => user.Email == email).FirstOrDefault();
         return(v != null);
     }
 }
Ejemplo n.º 5
0
        public ActionResult Registration([Bind(Exclude = "IsEmailVerified,ActivationCode")] User user)
        {
            bool   status  = false;
            string message = "";

            if (ModelState.IsValid)
            {
                #region Email or name already exists
                bool emailCheck    = EmailExist(user.Email);
                bool userNameCheck = UserNameExist(user.UserName);
                if (emailCheck)
                {
                    ModelState.AddModelError("EmailExists", "Email already exists");
                }
                if (userNameCheck)
                {
                    ModelState.AddModelError("UserNameExists", "User name already exists");
                }
                if (emailCheck || userNameCheck)
                {
                    return(View(user));
                }
                #endregion

                #region Generate activation code
                user.ActivationCode = Guid.NewGuid();
                #endregion

                #region Password hashing
                user.Password = System.Web.Helpers.Crypto.HashPassword(user.Password);
                System.Diagnostics.Debug.WriteLine(System.Web.Helpers.Crypto.VerifyHashedPassword(user.Password, "qwerty"));
                #endregion

                #region Save to DB
                using (JITContext db = new JITContext())
                {
                    user.IsEmailVerified = false;
                    db.Users.Add(user);
                    db.Configuration.ValidateOnSaveEnabled = false;
                    db.SaveChanges();
                    db.Configuration.ValidateOnSaveEnabled = true;
                }
                #endregion
                SendVerificationEmail(user.Email, user.ActivationCode.ToString());
                message = "Registration succsessfully done. Account activation link" +
                          "has been sent to your email: " + user.Email;
                status = true;
            }
            else
            {
                message = "Invalid Request";
            }
            ViewBag.Message = message;
            ViewBag.Status  = status;
            return(View(user));
        }