• 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。

  • 相关阅读:
    js用button激活 Alert 元素关闭按钮的交互功能
    js中的$符号代表什么
    js中#代表什么
    amazeui学习笔记--js插件(UI增强)--警告框Alert
    amazeui学习笔记--css(常用组件16)--文章页Article
    amazeui学习笔记--css(常用组件15)--CSS动画Animation
    amazeui学习笔记--css(常用组件14)--缩略图Thumbnail
    amazeui学习笔记--css(常用组件13)--进度条Progress
    amazeui学习笔记--css(常用组件12)--面板Panel
    amazeui学习笔记--css(常用组件11)--分页Pagination
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/4024899.html
Copyright © 2020-2023  润新知