asp.net 使用一般处理程序和ajax post实现登录以及记住密码
1.登录页面login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="loginDemo.aspx.cs" Inherits="WebThreadTest.loginDemo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script> <script src="js/jquery.cookie.js" type="text/javascript"></script> </head> <body> <div> 登录名 <input type="text" id="txtLoginName" /> <br /> 密码 <input type="password" id="txtPassword" /> <br /> <input type="checkbox" id="cbRemember" /> <label for="cbRemember">记住密码</label> <input type="submit" value="登录" onclick="login();"/> </div> <script type="text/javascript"> $(function(){ if ($.cookie("cbRemember") == "true") { $("#cbRemember").attr("checked", true); $("#txtLoginName").val($.cookie("loginName")); $("#txtPassword").val($.cookie("passWord")); //获取url中从?开始获取值: ?logout=true var params = window.location.search; var paramArray = params.split("logout="); //当不是退出时 执行自动登录 if(paramArray[1]!="true") { login(); } } }); function login(){ var loginName = $("#txtLoginName").val(); var password = $("#txtPassword").val(); var p = {}; p.loginName = loginName; p.password = password; $.post("login.ashx",p,function(r){ if(r=="ok") { if ($("#cbRemember").is(":checked")) { var txtLoginNameName = $("#txtLoginName").val(); var passWord = $("#txtPassword").val(); $.cookie("cbRemember", "true", { expires: 7 }); // 存储一个带7天期限的 cookie $.cookie("loginName", txtLoginNameName, { expires: 7 }); // 存储一个带7天期限的 cookie $.cookie("passWord", passWord, { expires: 7 }); // 存储一个带7天期限的 cookie } else { $.cookie("cbRemember", "false", { expires: -1 }); $.cookie("loginName", '', { expires: -1 }); $.cookie("passWord", '', { expires: -1 }); } //alert("登录成功"); window.location="/"; } else { $.cookie("cbRemember", "false", { expires: -1 }); $.cookie("loginName", '', { expires: -1 }); $.cookie("passWord", '', { expires: -1 }); alert("登录名或密码错误"); } }); } </script> </body> </html>
2、login.aspx.cs页面
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { object loginName = Session["loginName"]; object password = Session["password"]; if (loginName != null && password != null) { Response.Redirect("/"); } } }
3.login.ashx
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.Web.SessionState; namespace WebThreadTest { /// <summary> /// $codebehindclassname$ 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class login : IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string loginName = context.Request["loginName"]; string password = context.Request["password"]; if (loginName == "admin" && password == "123456") { context.Session["loginName"] ="admin"; context.Session["password"] = "123456"; context.Response.Write("ok"); } else { context.Response.Write("error"); } } public bool IsReusable { get { return false; } } } }
备注:
1.在aspx和aspx.cs中,都是以Session["xxx"]="aaa"和aaa=Session["xxx"].ToString()进行读写。而在ashx中,Session都要使用context.Session,读写方法不变。
2.在ashx文件中,若要对Session进行成功的读写,应该在使用Session的class后增加接口IRequiresSessionState(注意:需要引入:using System.Web.SessionState;),否则context.Session["xxx"]读出的总是null
那么,在aspx、aspx.cs和ashx中可以使用Session后,我们在AppCode的cs文件中,如何操作Session或者得到访问者的IP?
首先,aspx.cs 中是直接Request和Session,而在ashx中是context.Request和context.Session。aspx.cs中可以直接 使用,是因为有Web.UI的支持,而ashx中只能靠传进去的HttpContext实例对Request和Session等进行操作。那么同样 的,AppCode中的cs文件中也没有Web.UI,但是没有HttpContext参数,如果cs中可以得到当前的HttpContext,那么自然 而然就可以根据这个HttpContext去操作Request和Session。
HttpContext类中有一个静态属性叫Current,我们可以通过这个属性去得到当前的HttpContext。当然,在cs中要操作Session,也应该增加IRequiresSessionState接口。
与Session、Request、Response、Server等相关的方法和属性,都可以根据这个模式、方法去套用。
比如,在AppCode的cs文件中要得到当前访问者的IP,可以用HttpContext.Current.Request.UserHostAddress。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix