Example #1
0
        public ActionResult CreateUser(UserModel user)
        {
            TestDatabaseEntities context = new TestDatabaseEntities();

            var activationCode = Guid.NewGuid();
            var passwordHash   = Crypto.Hash(user.Password);
            var obj1           = context.UserLogins.Where(x => x.Username.Equals(user.Username)).FirstOrDefault();
            var obj2           = context.UserLogins.Where(x => x.Email.Equals(user.Email)).FirstOrDefault();

            if (obj1 != null)
            {
                if (string.Compare(user.Username, obj1.Username) == 0)
                {
                    ViewBag.message = "Username already exists. Please try again.";
                    return(RedirectToAction("CreateUser"));
                }
                else
                {
                    ViewBag.message = "Unknown error has occured.";
                    return(RedirectToAction("Index"));
                }
            }
            if (obj2 != null)
            {
                if (string.Compare(user.Email, obj2.Email) == 0)
                {
                    ViewBag.message = "Email already exists. Please try again.";
                    return(RedirectToAction("CreateUser"));
                }
                else
                {
                    ViewBag.message = "Unknown error has occured.";
                    return(RedirectToAction("Index"));
                }
            }


            else
            {
                context.Insert_User(user.Username, passwordHash, user.Email, activationCode, (int)user.UserGroup, (int)user.UserType);


                SendVerificationCodeEmail(user.Email, activationCode.ToString());

                return(RedirectToAction("AccountSuccess"));
            }
        }