<script type="text/javascript"language="javascript"> |
02 | functionIbtnEnter_onclick() { |
03 | checklogin(); |
04 | return false; |
05 | } |
06 | function checklogin() { |
07 | if($("#TxtUserName").val() == "") { |
08 | alert("用户名不能为空!"); |
09 | $("#TxtUserName").focus(); |
10 | return false; |
11 | } |
12 | if ($("#TxtPassword").val() == "") { |
13 | alert("密码不能为空!"); |
14 | $("#TxtPassword").focus(); |
15 | return false; |
16 | } |
17 | $.ajax({ |
18 | type: "POST", |
19 | url: "ajax/Handler.ashx?M=" + Math.random(), |
20 | data: "username=" + $("#TxtUserName").val().toString() + "&pwd="+ $("#TxtPassword").val().toString(), |
21 | success: function (data) { |
22 | if (data == "1") { |
23 | location.href = "index.aspx"; |
24 | return true; |
25 | } |
26 | else { |
27 | alert("请确认您输入的用户名或密码输入是否正确!"); |
28 | $("#TxtUserName").val(""); |
29 | $("#TxtPassword").val(""); |
30 | $("#TxtUserName").focus(); |
31 | return false; |
32 | } |
33 | } |
34 |
35 | }) |
36 | } |
37 | </script> |
服务端代码:
01 | 一般处理程序 |
02 |
03 | <%@ WebHandler Language="C#" Class="Handler" %> |
04 |
05 | using System; |
06 | using System.Web; |
07 | using System.Data.SqlClient; |
08 | using System.Web.SessionState;//继承接口IReadOnlySessionState需要引入的命名空间 |
09 |
10 | public class Handler : IHttpHandler, IRequiresSessionState |
11 | { |
12 | SqlHelper helper = new SqlHelper(); |
13 | public void ProcessRequest(HttpContext context) |
14 | { |
15 | context.Response.ContentType = "text/plain"; |
16 |
17 | string username = context.Request.Params["username"].ToString().Trim(); |
18 | string pwd = context.Request.Params["pwd"].ToString().Trim(); |
19 | if (username != "" && pwd != "") |
20 | { |
21 |
22 | string sql = @"SELECT * FROM [USER] WHERE USERNAME='"+username+"' AND PASSWORD='"+pwd+"' "; |
23 | if (!helper.Exists(sql)) |
24 | { |
25 | context.Response.Write("0"); |
26 | } |
27 |
28 | else |
29 | { |
30 | SqlDataReader reader = helper.ExecuteReader(sql); |
31 | while (reader.Read()) |
32 | { |
33 | context.Response.Write("1"); |
34 | context.Session["username"] = username.ToString().Trim(); |
35 | context.Session["pwd"] = pwd.ToString().Trim(); |
36 | } |
37 | } |
38 | |
39 | } |
40 | } |
41 | |
42 | public bool IsReusable |
43 | { |
44 | get |
45 | { |
46 | return false; |
47 | } |
48 | } |
49 | } |
本文详细介绍了网站用户登录过程中的验证逻辑与会话管理实现,包括用户名密码校验、Ajax异步请求处理及成功登录后的页面跳转。

1988

被折叠的 条评论
为什么被折叠?



