一个简单的后台验证程序(注册流程常用)
//在前台中,也有一些使用了已经封装后的验证(验证码验证),它自己封装了提交(form)方法。
注册 前台代码:
<form action="" method="post" name="form1" target="_blank"> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="tbclass"> <tr> <td> <table width="960" height="649" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="574" valign="top" class=""> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> </td> </tr> </table> <table width="85%" height="520" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td align="right"> </td> <td colspan="3" align="left" style="color: Red;"> @* @ViewBag.ErrorMsg*@ @*=========信息开始==========*@ @if (!string.IsNullOrEmpty(ViewData["user_id"].ToString())) { <input type="hidden" name="FAlipayID" value="@(ViewData["user_id"].ToString())" /> } @if (!string.IsNullOrEmpty(ViewData["user_name"].ToString())) { <input type="hidden" name="FAlipayName" value="@(ViewData["user_name"].ToString())" /> } @if (!string.IsNullOrEmpty(ViewData["token"].ToString())) { <input type="hidden" name="FAlipayToken" value="@(ViewData["token"].ToString())" /> } @*=========信息结束==========*@ </td> </tr> <tr> <td align="right"> <span class="register_mid_font_red"></span>@Html.LabelFor(m => Model.FName): </td> <td> @Html.TextBoxFor(m => Model.FName, new { @class = "register_input_bg", @maxlength = "16" }) </td> <td width="55%" class="grey_font_style">@Html.ValidationMessageFor(m => Model.FName) </td> </tr> <tr> <td align="right"> <span class="register_mid_font_red"></span>@Html.LabelFor(m => Model.FPassWord): </td> <td> @Html.PasswordFor(m => Model.FPassWord, new { @class = "register_input_bg", @maxlength = "20" }) </td> <td class="grey_font_style">@Html.ValidationMessageFor(m => Model.FPassWord) </td> </tr> <tr> <td align="right"> <span class="register_mid_font_red"></span>@Html.LabelFor(m => Model.ConfirmPassword): </td> <td> @Html.PasswordFor(m => Model.ConfirmPassword, new { @class = "register_input_bg", @maxlength = "20" }) </td> <td class="grey_font_style">@Html.ValidationMessageFor(m => Model.ConfirmPassword) </td> </tr> <tr> <!--找回密码的方式--> <tr> <td height="22" align="right" class="grey_font_style2"> <select class="wd" id="sel" onchange="chg()" size="1" style="font-family: Verdana,Arial; font-size: 10pt;"> <option value="FPhone">电话号码</option> <option value="FMail">电子邮箱</option> <option value="FSafeQuestion">安全问题</option> </select> <td height="22" align="right" class="grey_font_style2" id="change"> <input type="text" value="" name="FPhone" id="FPhone" size="1" style="font-family: Verdana,Arial; font-size: 10pt font-color: #CCC" class="wd2" /> <input type="text" value="" name="FMail" id="FMail" style="display: none; font-family: Verdana,Arial; font-size: 10pt;" class="wd2" /> <select name="FSafeQuestion" id="FSafeQuestion" size="1" style="display: none; font-family: Verdana,Arial; font-size: 10pt;" class="wd2"> <option value="" selected="selected">请选择</option> <option value="我的小学名字">我的小学名字?</option> <option value="我妈妈的名字">我妈妈的名字?</option> <option value="我最难忘的日子">我最难忘的日子?</option> <option value="我的学号或(工号)是">我的学号或(工号)是?</option> <option value="我配偶的姓名是">我配偶的姓名是?</option> <option value="我最喜欢的休闲运动是">我最喜欢的休闲运动是?</option> <option value="我最喜欢的歌曲">我最喜欢的歌曲?</option> <option value="我最喜爱的电影">我最喜爱的电影?</option> <option value="自己编写问题">自己编写问题?</option> </select> </td> </td> </tr> <tr id="key" style="display: none;"> <td align="right"> <span class="register_mid_font_red"></span>你的答案是: </td> <td> <input type="text" value="" name="FSafeAnswer" id="FSafeAnswer" style="font-family: Verdana,Arial; font-size: 10pt;" class="wd2" /> </td> </tr> <!--验证码--> <tr> <td height="61" align="right"> <span class="register_mid_font_red"></span>@Html.LabelFor(m => Model.FVerifyImg): </td> <td> @Html.TextBoxFor(m => Model.FVerifyImg, new { @class = "register_input_bg1", @maxlength = "4" }) <img src="@Url.Action("VirifyImg")" id="verify_code" alt="看不清?点击换一个" onclick="this.src=this.src+'?'" style="cursor:pointer;" /> @Html.ValidationMessageFor(m => Model.FVerifyImg) </td> <td> </td> </tr> <tr> <td height="45"> </td> <td colspan="2"> <table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="4%"> <input type="checkbox" name="checkbox" id="checkbox_xy" checked="checked" /> <label for="checkbox"> </label> </td> <td width="96%"> <span class="grey_font_style"> <label for="checkbox"> <a href="UserGreement" target="_blank">我已阅读并同意相关服务条款</a> </label> </span> <label for="checkbox"> </label> </td> </tr> <!--注册按钮--> <tr> <td height="84" colspan="3" align="left"> <input type="submit" id="submit" value=" 立即注册 " class="zhuce "/> <span class="grey_font_style_phoneandmail" style="font-family: Verdana,Arial; font-size: 14pt;">@ViewBag.ErrorMsg1</span><!--手机号已被使用--> </td> </tr> </table> </td> </tr> <tr> <td height="123"> </td> <td colspan="2" valign="middle"> </td> </tr> </table> </td> </tr> <tr> <td height="14" valign="top"> <img src="/images/index/register_down.gif" alt="" width="960" height="14" /> </td> </tr> </table> </td> </tr> </table> </form>
注册 后台代码:
[HttpGet] public ActionResult Add(string user_name = "", string user_id = "", string token = "") { ViewData["user_name"] = user_name; ViewData["user_id"] = user_id; ViewData["token"] = token; return View(); } [HttpPost] public ActionResult Add(UserInfo model, UserPersonal model2, string user_name = "", string user_id = "", string token = "", string FPhone = "", string FMail = "", string FSafeQuestion = "", string FSafeAnswer = "") { //=======更为为后台验证session形式验证 验证码 --- 0=======> ViewData["user_name"] = user_name; ViewData["user_id"] = user_id; ViewData["token"] = token; ViewData["FPhone"] = FPhone; ViewData["FMail"] = FMail; ViewData["FSafeAnswer"] = FSafeAnswer; ViewData["FSafeQuestion"] = FSafeQuestion; if (Session["checkcode"] != null && model.FVerifyImg.Trim() == Session["checkcode"].ToString()) { UserInfoBLL userinfobll = new UserInfoBLL(); UserPersonalBLL userpersonalbll = new UserPersonalBLL(); UserInfo userinfo = userinfobll.FindALL().Where<UserInfo>(s => s.FName == model.FName.Trim()).FirstOrDefault(); UserPersonal userpersonal = userpersonalbll.FindALL().Where<UserPersonal>(s => s.FId == model2.FId.Trim()).FirstOrDefault(); if (userinfo == null && userpersonal == null) { if (UserInfoBLL.Two_yanzheng(FPhone) == "0" && UserInfoBLL.Two_yanzheng(FMail) == "0") { model.FId = TableUtils.SetFID(); model.FPassWord = EncryptUtil.MD5String(model.FPassWord.Trim()).Trim(); //对密码进行加密 // model.FPassWord = model.FPassWord; model.FNoWorryId = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString(); model.FCreatorId = "-1";//自注册用户 model.FIsDelete = false;//表示未被删除 model.FCreateTime = DateTime.Now; model.FUpdatorId = "-1";//自注册用户 model.FUpdateTime = DateTime.Now; model.FInfoIsFinished = "0";//0资料未完善,1资料完善未审核,2审核通过,3审核未通过 model.FState = 0; model.FIsAdmin = 0; model2.FId = model.FId; if (userinfobll.Create(model)) { Session["TypePhone"] = FPhone; Session["TypeMail"] = FMail; bool result = userpersonalbll.Create(model2); } Session["Userid"] = model.FId; return RedirectToAction("Success/" + model.FNoWorryId + ""); } else { UserPersonal user = userpersonalbll.FindALL().Where(x => x.FPhone == model2.FPhone).FirstOrDefault(); UserPersonal user1 = userpersonalbll.FindALL().Where(x => x.FMail == model2.FMail).FirstOrDefault(); if (user != null) { ViewBag.ErrorMsg1 = "该手机号已被注册使用!请更换注册手机号!"; } if (user1 != null) { ViewBag.ErrorMsg1 = "该邮箱号已被注册使用!请更换注册邮箱号!"; } // ViewBag.ErrorMsg2 = "该邮箱号已被注册使用!"; return View(); } } else { //ViewBag.ErrorMsg = "该用户已存在!"; // ViewBag.ErrorMsg2 = "该用户已存在!"; return View(model2); } } else { ViewBag.ErrorMsg = "验证码不正确!"; return View(model); } }
在UserInfoBLL中实现验证方法 Two_yanzheng() : //作用是判断数据在数据库中是否存在,而后,在前台页面中 做 XXX 已存在的 验证提示!
//-------------------------------验证手机号和邮箱号是否已存在---------------------------------------// public static string Two_yanzheng(string RequestText) { string result = ""; using (UserPersonalBLL bll = new UserPersonalBLL()) { UserPersonal UserPhone = bll.FindALL().Where(x => x.FPhone == RequestText).FirstOrDefault(); UserPersonal UserEmail = bll.FindALL().Where(x => x.FMail == RequestText).FirstOrDefault(); if (UserPhone == null && UserEmail == null) //&& UserEmail == null { result = "0"; } else { result = "1"; } } return result; }
有些梦想是遥不可及的,除非你坚持!