登錄界面是信息系統(tǒng)提供的必備的功能,是提供給用戶提供維護(hù)信息的接口。接下來,我來帶領(lǐng)大家打造一個(gè)漂亮、安全的登錄界面,使用的技術(shù)是ASP.NET+jQuery
先來看看預(yù)覽效果
Ajax登錄重點(diǎn)在Ajax,輸入用戶名和密碼后,使用Ajax方式將信息提交到服務(wù)器端,服務(wù)器端判斷時(shí)候存在該用戶,存在則登錄成功并轉(zhuǎn)向管理界面(有時(shí)需要寫cookie或是利用Session,此處不作討論),不存在則提示登錄失敗。
基本流程圖如下
上面是主要思路,為了打造安全的登錄,在使用ajax將密碼傳到服務(wù)器端前,我們可以使用MD5對密碼進(jìn)行加密,當(dāng)然數(shù)據(jù)庫中存儲的也是加密后的字符串。jQuery有一款這樣的MD5加密插件,使用十分方便。
流程知道了,就可以方便實(shí)現(xiàn)了。以下是一些主要的代碼
Default.aspx:主要是提供超鏈接,點(diǎn)擊會調(diào)用thickbox,打開彈出頁面。
<div style="margin-left:50px; margin-top:50px; ">歡迎使用后臺,<a href="Login.htm?TB_iframe&height=180&width=350&modal=true" class="thickbox" id="myToolTip" title="點(diǎn)擊登錄,進(jìn)入后臺管理" >點(diǎn)擊登錄!</a> 繼續(xù)瀏覽前臺,<a href="../Default.aspx">返回前臺</a>
login.htm:真正的登錄界面,負(fù)責(zé)登錄邏輯
<script type="text/javascript" src="js/jquery-1.3.2.js"></script><script type="text/javascript"> $().ready(function () { $('#Login').click(function () { if ($('#username').val() == "" || $('#password').val() == "") { alert("用戶名或密碼不能為空!"); } else { $.ajax({ type: "POST", url: "Ajax/LoginHandler.ashx", data: "username=" + escape($('#username').val()) + "&password=" + escape($('#password').val()), beforeSend: function () { $("#loading").css("display", "block"); //點(diǎn)擊登錄后顯示loading,隱藏輸入框 $("#login").css("display", "none"); }, success: function (msg) { $("#loading").hide(); //隱藏loading if (msg == "success") { //parent.tb_remove(); parent.document.location.href = "admin.htm"; //如果登錄成功則跳到管理界面 parent.tb_remove(); } if (msg == "fail") { alert("登錄失??!"); } }, complete: function (data) { $("#loading").css("display", "none"); //點(diǎn)擊登錄后顯示loading,隱藏輸入框 $("#login").css("display", "block"); }, error: function (XMLHttpRequest, textStatus, thrownError) { } }); } }); });</script><div id="loading" style="text-align: center; display: none; padding-top: 10%"> <img src="images/loadingajax.gif" alt="loading" /></div><div id="login" style="text-align: center"><div style="position:absolute; right:0; top:0"><img src="images/closebox.png" onclick="parent.tb_remove()" alt="點(diǎn)擊關(guān)閉" style="cursor:pointer" /></div> <table border="0" cellpadding="3" cellspacing="3" style="margin: 0 auto;"> <tr> <td style="text-align: right; padding: 10px"> <label> 用戶名:</label> </td> <td> <input id="username" type="text" size="20" /> </td> </tr> <tr> <td style="text-align: right; padding: 10px"> <label> 密碼:</label> </td> <td> <input id="password" type="password" size="20" /> </td> </tr> <tr align="right"> <td colspan="2"> <input type="submit" id="Login" value=" 登 錄 " style="margin-right: 50px"> <input type="submit" id="LoginCancel" value=" 取 消 " onclick="parent.tb_remove()"> </td> </tr> </table></div>
新聞熱點(diǎn)
疑難解答
圖片精選