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。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM