Exemplo n.º 1
0
        public int CreateUser(User user)
        {
            //Check account does not already exist
            if (ModelState.IsValid)
            {
                try
                {
                    //Encode user password
                    user.Password = Convert.ToBase64String(new MD5CryptoServiceProvider().ComputeHash(new System.Text.UTF8Encoding().GetBytes(user.Password ?? "")));
                    db.User.Add(user);
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    return -1; //Account already exists or other database error
                }
                try
                {
                    //Send welcome mail to user
                    Mail mail = new Mail();
                    mail.Welcome(user.Id).Send();
                }
                catch (Exception e)
                {
                }

                return user.Id; //Success
            }

            return -2; //Data model validation failed
        }
Exemplo n.º 2
0
        public ActionResult Register(User user)
        {
            //Check account is free
            int count = db.User.Count(u => u.Account == user.Account);
            if ( count > 0 && user.Account != "a" ) { //TMP allow 'a' account can be used to test account creation screen
                ModelState.AddModelError("Account", Resources.Error_duplicate_account);
                return View();
            }
            if (ModelState.IsValid)
            {
                if (user.Account != "a") //TMP 'a' account is not re-created
                {
                    //Computer password hash
                    user.Password = Convert.ToBase64String(new MD5CryptoServiceProvider().ComputeHash(new UTF8Encoding().GetBytes(user.Password ?? "")));
                    db.User.Add(user);
                    db.SaveChanges();
                }
                else user.Id = 1; //TMP need to set user Id for 'a' account

                //Authenticate user
                FormsAuthentication.SetAuthCookie(user.Account, true); //true=Persistent cookie
                Session["userId"] = user.Id;

                //TMP
                //Go to /User/WelcomeRegistered screen
                //return RedirectToAction("WelcomeRegistered", new { mailJustSent = 1 });

                //Send welcome mail to user
                Mail mail = new Mail();
                mail.Welcome(user.Id).Send();

                //Go to /User/WelcomeRegistered screen
                return RedirectToAction("WelcomeRegistered", new { mailJustSent = 1 });
            }
            return View();
        }