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。

 

 

转载于:https://www.cnblogs.com/zoro-zero/p/4024899.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值