示例#1
0
        public ActionResult Register(Users userToCreate)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            try
            {
                var users = (from m in _db.Users
                             where m.username == userToCreate.username
                             select m);

                if (users.Count() != 0)
                {
                    TempData["ErrorMessage"] = "User name exists! ";
                    return(View());
                }
                if (userToCreate.upasswd != Request.Form["ConfirmPassword"])
                {
                    TempData["ErrorMessage"] = "Registration failed! Your passwords must match, please re-enter and try again.";
                    return(View());
                }

                try
                {
                    string content = System.IO.File.ReadAllText(Server.MapPath("~/NewMemberEmail.txt"));
                    content = content.Replace("[Name]", userToCreate.username);
                    content = content.Replace("[LINK]", "<a href='http://" + Request.Url.Host + ":" + Request.Url.Port + "/User/Activation-" + Server.UrlEncode(userToCreate.username) + "-" + MD5Code.getMd5Hash(userToCreate.username) + "'>^_^Active^_^</a>");
                    content = content.Replace("[UserName]", userToCreate.username);
                    content = content.Replace("[Pwd]", userToCreate.upasswd);

                    if (!SendMail.send(userToCreate.uemail, content, Server, "Active"))
                    {
                        TempData["ErrorMessage"] = "Sorry. The format of your email address can't be recognized.";
                        return(View());
                    }
                    ;
                }
                catch (Exception ex)
                {
                    TempData["ErrorMessage"] = "Registration failed! Check your email again please." + ex.Message;
                    return(View());
                }

                try
                {
                    string key = userToCreate.username;
                    while (key.Length < 8)
                    {
                        key = key + key;
                    }
                    userToCreate.upasswd = DESCode.EncryptDES(userToCreate.upasswd, key);
                    _db.AddToUsers(userToCreate);
                    _db.SaveChanges();
                    TempData["SuccessMessage"] = "Registration succeeds! Your can log in using the new username and password.";
                }
                catch (Exception ex)
                {
                    TempData["ErrorMessage"] = "The databse is unreachable. Try again later." + ex.Message;
                    return(View());
                }

                return(RedirectToAction("Main"));
            }
            catch (Exception exception)
            {
                TempData["ErrorMessage"] = "Registration has failed because: " + exception.Message;
                return(View());
            }
        }