Implementation of ASP. NET jQuery Ajax User Login function

Source: Internet
Author: User
Main Page Call Code snippet:

<asp:hyperlink id= "Lnklogin" runat= "Server" navigateurl= "#" > Login </asp:HyperLink> <script language= " JavaScript "type=" Text/javascript "> $ (' #<%=this.lnklogin.clientid%> '). Click (function () {Jbox.open (' Iframe-jboxid ', ' iframe ', ' login.aspx ', ' User login ', ' width=400,height=250,center=true,draggable=true,model=true '); } ); </script>

Login.aspx Code:

<form id= "Form1" onsubmit= "return false;" > <table id= "login-table" > <tr> <td width= "max" > Study number:</td> <td><input class= "textbox "Type=" text "style=" width:160px; "id=" txtUserName "maxlength=" 9 "onblur=" Checkusername () "Onclick=" $.trim ( This.value) "/><span></span> </td> </tr> <tr> <td width=" "> Password:</td> <td><input class= "textbox" type= "Password" style= "width:160px; id=" Txtuserpwd "onblur=" Checkuserpwd () " onclick= "$.trim (this.value)"/><span></span> </td> </tr> <tr> <td width= "" > Captcha:</td> <td><input class= "textbox" type= "text" style= "width:160px;" maxlength= "5" id= "Txtcheckcode" Onblur= "Checkcheckcode ()" onclick= "$.trim (this.value)"/><span> </span> </td> </tr> < tr> <td width= "></td> <td><div style=" color: #808080; > characters in input, case insensitive </div><br/>  <a href=" # "id=" Change_image "> not clear, change a piece </a></ td> </tr> <tr> <td width= "Up" ></td> <td><input type= "image" src= "app_themes/ Images/btn_login.jpg "id=" Btnlogin "alt=" immediately login "style=" border:0; " /></td> </tr> </table> </form>

JQuery Code:

<script language= "javascript" type= "Text/javascript" > $ (document). Ready (function () {//AUTH code update $ (' #change_image ') ). Click (function () {$ (' #imgCheckCode '). attr (' src ', ' checkcode.aspx? ') +math.random ()); }); Key code $ ("#btnLogin"). Click (function () {if (Checkusername () && checkuserpwd () && Checkcheckcode ()) { var data = {UserName: $ (' #txtUserName '). Val (), Userpwd: $ (' #txtUserPwd '). Val (), Checkcode: $ (' #txtCheckCode '). Val ()}; Submit data to Login.ashx page processing $.post ("Ajax/login.ashx", data,function (Result) {if (result = = "1")//Login succeeded {alert ("Login successful! You can do something else! "); Close the Simulation window window.parent.window.jBox.close (); } else if (result = = "2")//Authenticode Error {$ (' #txtCheckCode '). Next ("span"). CSS ("Color", "red"). Text ("* captcha error");} else {alert ("Login missing Defeat! Please try again "); } }); } else {checkusername (); Checkuserpwd (); Checkcheckcode ();}}); }); Check the UserName function Checkusername () {if ($ ("#txtUserName"). Val (). length = = 0) {$ ("#txtUserName"). Next ("span") . CSS ("Color", "red"). Text ("* User name is not empty"); return false; } else {VAR reg =/^\d{9}$/; if (!reg.test (' #txtUserName '). Val ())) {$ (' #txtUserName '). Next ("span"). CSS ("Color", "red"). Text ("* Correct format such as: 030602888 "); return false; } else {$ ("#txtUserName"). Next ("span"). CSS ("Color", "red"). Text (""); return true; }}}//check the PWD function checkuserpwd () {if ($ (' #txtUserPwd '). Val (). length = = 0) {$ (' #txtUserPwd '). Next ("span"). CSS ("Color", "red"). Text ("* Password is not empty"); return false; } else {$ (' #txtUserPwd '). Next ("span"). CSS ("Color", "red"). Text (""); return true; }}//Check the check code function Checkcheckcode () {if ($ (' #txtCheckCode '). Val (). length = = 0) {$ (' #txtCheckCode '). Next ("span"). CSS ("Color", "red"). Text ("* Verification code is not empty"); return false; } else {$ (' #txtCheckCode '). Next ("span"). CSS ("Color", "red"). Text (""); return true; }} </script>

Login.ashx Code:

Using System; Using System.Collections; Using System.Data; Using System.Linq; Using System.Web; Using System.Web.Services; Using System.Web.Services.Protocols; Using System.Xml.Linq; Using System.Data.SqlClient; Using System.Web.SessionState; Support session must reference namespace Website.ajax {[WebService (namespace = "http://tempuri.org/")] [WebServiceBinding ( ConformsTo = wsiprofiles.basicprofile1_1)] public class Login:ihttphandler,irequiressessionstate {public void ProcessR Equest (HttpContext context) {context. Response.ContentType = "Text/plain"; String checkcode = ""; if (context. session["Checkcode"]! = null) {Checkcode = convert.tostring (context. session["Checkcode"]). ToLower (); } if (context. request.form["Checkcode"].  ToLower () = = Checkcode) {using (SqlConnection conn = new SqlConnection (sqlhelper.studentconnectionstring)) {String sql = "Select Id,stunumber,userpassword,realname from T_stuuser where stunumber= @UserName and userpassword= @UserPwd"; SqlCommand cmd = new SqlCommand (sql, conn); SqlParameter pusername = cmd. Parameters.Add ("@UserName", SqlDbType.VarChar, 30); SqlParameter puserpwd = cmd. Parameters.Add ("@UserPwd", SqlDbType.VarChar, 150); Pusername.value = context. request.form["UserName"]; Puserpwd.value = COMMON.MD5 (context. request.form["Userpwd"]); Conn. Open (); SqlDataReader SDR = cmd. ExecuteReader (commandbehavior.closeconnection); if (SDR). Read ()) {context. session["UserID"] = convert.tostring (sdr["ID"]); Context. session["stuname"] = convert.tostring (sdr["realname"]); Context. session["Stunumber"] = convert.tostring (sdr["Stunumber"]); Context. Response.Write ("1"); Login Successful} else {context. Response.Write ("0"); Login failed, user name or password error}} else {context. Response.Write ("2"); Authenticode error}} public bool IsReusable {get {return false;}}} }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.