public static bool InsertUser(User user) { using (var mongo=Mongo.Create(@"mongodb://localhost/test")) { var userCollection = mongo.GetCollection<User> (); var oldUsers = userCollection.AsQueryable ().Where (User => User.Name == user.Name).ToList (); if (oldUsers.Count>0) { throw new ItemExistException ("已经使用过的昵称"); } var oldUsers2 = userCollection.AsQueryable ().Where (User =>User.Mail == user.Mail).ToList (); if (oldUsers2.Count>0) { throw new ItemExistException ("已经使用过的邮件地址"); } userCollection.Insert (user); return true; } }
//注册页面的试图函数 public ActionResult Signup() { if (Request.Form["Mail"]!=null&&Request.Form["Name"]!=null&&Request.Form["Password"]!=null&&Request.Form["Password2"]!=null) { if (Request.Form["Mail"]!=""&&Request.Form["Name"]!=""&&Request.Form["Password"]!=""&&Request.Form["Password2"]!="") { bool isValidated=true; Regex mailExp = new Regex(@"^\w+@\w+(\.\w+)+(\,\w+@\w+(\.\w+)+)*$"); if (mailExp.Match(Request.Form["Mail"].ToString()).Success == false||Request.Form["Mail"].Length>50) { isValidated=false; ViewData["Message"]+="无效的邮件地址<br />"; } if (Request.Form["Name"].Length>12) { isValidated=false; ViewData["Message"]+="无效的昵称<br />"; } if (Request.Form["Password"]!=Request.Form["Password2"]||Request.Form["Password"].Length>20||Request.Form["Password"].Length<6) { isValidated=false; ViewData["Message"]+="无效的密码<br />"; } if (isValidated==true) { User user=new User() { Name=Request.Form["Name"], Password=Request.Form["Password"], Mail=Request.Form["Mail"], Permission=0, }; try { UserFarm.InsertUser(user); } catch (Exception e) { ViewData["Message"]+=e.Message; return View(); } Session["Id"]=user.Id; Session["Name"]=user.Name; return Redirect("/i/"+user.Id+"/"); } } } return View(); }