ASP.NET会员注册登录模块(MD5加密,Parameters防止SQL注入,判断是否注册)
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET会员注册登录模块(MD5加密,Parameters防止SQL注入,判断是否注册)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
MD5加密,Parameters防止SQL注入: protected?void?btnLog_Click(object?sender,?EventArgs?e) ????{ ????????//獲取驗證碼 ????????string?code?=?txtCode.Text; ????????//判斷用戶輸入的驗證碼是否正確 ????????if?(Request.Cookies["CheckCode"].Value?==?code) ????????{ ????????????//創(chuàng)建數(shù)據(jù)庫連接 ????????????SqlConnection?con?=?new?SqlConnection("server=.;database=db_Register;uid=sa;pwd=102545;"); ????????????//打開數(shù)據(jù)庫連接 ????????????con.Open(); ????????????//使用MD5加密將用戶輸入的密碼加密 ????????????string?pass?=?FormsAuthentication.HashPasswordForStoringInConfigFile(txtUserpass.Text,?"MD5"); ????????????//創(chuàng)建SQL語句,該語句用來查詢用戶輸入的用戶名和密碼是否正確 ????????????string?sqlSel?=?"select?count(*)?from?tb_userInfo?where?userName=@name?and?userPass=@pass"; ????????????//創(chuàng)建SqlCommand對象 ????????????SqlCommand?com?=?new?SqlCommand(sqlSel,?con); ????????????//使用Parameters的add方法添加參數(shù)類型,防止SQL注入,Parameters屬性傳參的方法將非法字符過濾掉. ????????????com.Parameters.Add(new?SqlParameter("name",?SqlDbType.VarChar,?20)); ????????????//設(shè)置Parameters的參數(shù)值 ????????????com.Parameters["name"].Value?=?txtUserName.Text; ????????????com.Parameters.Add(new?SqlParameter("pass",?SqlDbType.VarChar,?50)); ????????????com.Parameters["pass"].Value?=?pass; ????????????//判斷ExecuteScalar方法返回的參數(shù)是否大于0大于表示登錄成功并給出提示 ????????????if?(Convert.ToInt32(com.ExecuteScalar())?>?0) ????????????{ ????????????????RegisterStartupScript("",?"<script>alert('登錄成功!')</script>"); ????????????????//清空文本框 ????????????????txtCode.Text?=?txtUserName.Text?=?""; ????????????} ????????????else ????????????{ ????????????????RegisterStartupScript("",?"<script>alert('用戶名或密碼錯誤!')</script>"); ????????????} ????????} ????????else ????????{ ????????????RegisterStartupScript("",?"<script>alert('驗證碼輸入錯誤!')</script>"); ????????} ????} 設(shè)置密碼強度: function?passHint() ????{ ????????var?txt=document.getElementById('txtPass').value; ????????if(txt.length<6) ????????{ ????????????document.all("tab").rows[0].cells[1].bgColor="red"; ????????????document.all("tab").rows[0].cells[2].bgColor=""; ????????}else ????????{ ????????document.all("tab").rows[0].cells[2].bgColor="red"; ????????????????document.all("tab").rows[0].cells[1].bgColor=""; ????????} ???????? ????}
<table?id="tab"?border="0"?cellpadding="0"?cellspacing="0"?style="width:?182px"> ????<tr> ????????<td?style="width:?276px"> ????????????<span?style="font-size:?10pt">密碼強度:</span></td> ????????<td?id="r"?style="width:?100px"> ????????????<asp:Label?ID="labEbb"?runat="server"?Text="弱"?Width="18px"?Font-Size="12px"></asp:Label></td> ????????<td?style="width:?92px"> ????????????<asp:Label?ID="labStrong"?runat="server"?Text="強"?Width="18px"?Font-Size="12px"></asp:Label></td> ????????<td?style="width:?106px"> ????????????</td> ????</tr> </table> 判斷是否注冊: 前臺代碼: <asp:UpdatePanel?ID="UpdatePanel1"?runat="server"> ????<ContentTemplate> ????????<table?border="0"?cellpadding="0"?cellspacing="0"?style="width:?477px;?height:?48px;"> ????????????<tr> ????????????????<td?style="width:?88px;?height:?24px;?text-align:?right"> ????????????????????<span?style="font-size:?10pt">會?員?名:?</span> ????????????????</td> ????????????????<td?style="height:?24px;?text-align:?left;?width:?509px;"?colspan="2"> ????????????????????<asp:TextBox?onFocus="tName();"?ID="txtName"?runat="server"?Width="89px"?AutoPostBack="True" ????????????????????????OnTextChanged="txtName_TextChanged"></asp:TextBox><span?style="color:?#ff0000">*</span><asp:RequiredFieldValidator ????????????????????????????ID="rfvName"?runat="server"?ErrorMessage="用戶名不能為空"?Width="1px" ????????????????????????????ControlToValidate="txtName">*</asp:RequiredFieldValidator> ????????????????????<asp:Label?ID="labUser"?runat="server"?Text="只能輸入數(shù)字、字母、下劃線"?Width="159px"?Font-Size="12px"></asp:Label> ????????????????????<asp:Label?ID="labIsName"?runat="server"?Font-Size="12px"></asp:Label></td> ????????????</tr> ????????</table> ????</ContentTemplate> </asp:UpdatePanel> 后臺代碼: ????protected?bool?isName() ????{ ????????//創(chuàng)建一個布爾型變量并初始化為false; ????????bool?blIsName?=?false; ????????//創(chuàng)建SQL語句,該語句用來判斷用戶名是否存在 ????????string?sqlSel?=?"select?count(*)?from?tb_userInfo?where?userName='"?+?txtName.Text?+?"'?"; ????????//創(chuàng)建數(shù)據(jù)庫連接 ????????SqlConnection?con?=?new?SqlConnection("server=.;database=db_Register;uid=sa;pwd=102545;"); ????????//打開數(shù)據(jù)庫連接 ????????con.Open(); ????????//創(chuàng)建SqlCommand對象 ????????SqlCommand?com?=?new?SqlCommand(sqlSel,?con); ????????//判斷ExecuteScalar方法返回的參數(shù)是否大于0,大于表示用戶名已存在 ????????if?(Convert.ToInt32(com.ExecuteScalar())?>?0) ????????{ ????????????blIsName?=?true; ????????} ????????else ????????{?????????????????????? ????????????blIsName?=?false; ????????} ????????//返回布爾值變量 ????????return?blIsName; ????} ????protected?bool?isNameFormar() ????{ ????????//創(chuàng)建一個布爾型變量并初始化為false; ????????bool?blNameFormar?=?false; ????????//設(shè)置正則表達式 ????????Regex?re?=?new?Regex("^\\w+$"); ????????//使用Regex對象中的IsMatch方法判斷用戶名是否滿足正則表達式 ????????if?(re.IsMatch(txtName.Text)) ????????{ ????????????//設(shè)置布爾變量為true ????????????blNameFormar?=?true; ????????????//設(shè)置label控件的顏色 ????????????labUser.ForeColor?=?System.Drawing.Color.Black; ????????} ????????else ????????{ ????????????labUser.ForeColor?=?System.Drawing.Color.Red; ????????????blNameFormar?=?false; ????????} ????????//返回布爾型變量 ????????return?blNameFormar; ????} ????protected?void?txtName_TextChanged(object?sender,?EventArgs?e) ????{ ????????//判斷用戶名是否為空 ????????if?(txtName.Text?==?"") ????????{ ????????????//使用Label控件給出提示 ????????????labIsName.Text?=?"用戶名不能為空"; ????????????//設(shè)置Label控件的顏色 ????????????labIsName.ForeColor?=?System.Drawing.Color.Red; ????????} ????????else ????????{ ????????????//調(diào)用自定義isNameFormar方法判斷用戶名是否滿足格式要求 ????????????if?(isNameFormar()) ????????????{ ????????????????//調(diào)用isName自定義方法判斷用戶名是否已注冊 ????????????????if?(isName()) ????????????????{ ????????????????????labIsName.Text?=?"用戶名已存在!"; ????????????????????labIsName.ForeColor?=?System.Drawing.Color.Red; ????????????????} ????????????????else ????????????????{ ????????????????????labIsName.Text?=?"可以注冊!"; ????????????????????labIsName.ForeColor?=?System.Drawing.Color.Blue; ????????????????} ????????????} ????????????else ????????????{ ????????????????labIsName.Text?=?""; ????????????} ????????} ????}
轉(zhuǎn)載于:https://www.cnblogs.com/zt102545/p/3321231.html
總結(jié)
以上是生活随笔為你收集整理的ASP.NET会员注册登录模块(MD5加密,Parameters防止SQL注入,判断是否注册)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 提高你的Java代码质量吧:使用valu
- 下一篇: asp.net模态窗口返回值