博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#用AJAX验证用户登陆 使用三层结构
阅读量:6690 次
发布时间:2019-06-25

本文共 9299 字,大约阅读时间需要 30 分钟。

DAL:

using
 System;
using
 System.Collections.Generic;
using
 System.Configuration;
using
 System.Linq;
using
 System.Text;
using
 System.Data;
using
 System.Data.Common;
using
 System.Data.SqlClient;
using
 System.Xml;
using
 System.Xml.Xsl;
using
 System.Xml.XPath;
namespace
 DAL
ExpandedBlockStart.gifContractedBlock.gif
{
    
public static class DBHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 创建DbCommand的方法
        
/// </summary>
        
/// <returns></returns>
        public static DbCommand CreateCommand()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string connectionString = ConfigurationManager.AppSettings["connString"];
            
string providerName = ConfigurationManager.AppSettings["provName"];
            DbProviderFactory factory 
= DbProviderFactories.GetFactory(providerName);
            DbConnection conn 
= factory.CreateConnection();
            conn.ConnectionString 
= connectionString;
            DbCommand cmd 
= conn.CreateCommand();
            
return cmd;
        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 返回DataTabale的方法,第一重重载
        
/// </summary>
        
/// <param name="sql"></param>
        
/// <returns></returns>
        public static DataTable ExecuteCommand(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            DataTable table 
= new DataTable();
            DbDataReader reader 
= null;
            DbCommand cmd 
= DBHelper.CreateCommand();
            cmd.CommandText 
= sql;
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
                cmd.Connection.Open();
                reader 
= cmd.ExecuteReader();
                table.Load(reader);
            }
            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw ex;
            }
            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                reader.Close();
                cmd.Connection.Close();
            }
            
return table;
        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 返回DataTabale的方法,第二重重载,可执行存储过程
        
/// </summary>
        
/// <param name="sql"></param>
        
/// <param name="values"></param>
        
/// <returns></returns>
        public static DataTable ExecuteCommand(string sql, params DbParameter[] values)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            DataTable table 
= new DataTable();
            DbDataReader reader 
= null;
            DbCommand cmd 
= DBHelper.CreateCommand();
            cmd.CommandText 
= sql;
            cmd.CommandType 
= CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                cmd.Connection.Open();
                reader 
= cmd.ExecuteReader();
                table.Load(reader);
            }
            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw ex;
            }
            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                reader.Close();
                cmd.Connection.Close();
            }
            
return table;
        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 执行修改、删除、添加数据库中表的方法,第一重重载
        
/// </summary>
        
/// <param name="sql"></param>
        
/// <returns></returns>
        public static int ExecuteNoQuery(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int affect = -1;
            DbCommand cmd 
= DBHelper.CreateCommand();
            cmd.CommandText 
= sql;
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                cmd.Connection.Open();
                affect 
= cmd.ExecuteNonQuery();
            }
            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw ex;
            }
            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                cmd.Connection.Close();
            }
            
return affect;
        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 执行修改、删除、添加数据库中表的方法,第二重重载,可执行存储过程
        
/// </summary>
        
/// <param name="sql"></param>
        
/// <param name="values"></param>
        
/// <returns></returns>
        public static int ExecuteNoQuery(string sql,params DbParameter[] values)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int affect = -1;
            DbCommand cmd 
= DBHelper.CreateCommand();
            cmd.CommandText 
= sql;
            cmd.CommandType 
= CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                cmd.Connection.Open();
                affect 
= cmd.ExecuteNonQuery();
            }
            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw ex;
            }
            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                cmd.Connection.Close();
            }
            
return affect;
        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 返回数据表的第一行第一列,第一重重载
        
/// </summary>
        
/// <param name="sql"></param>
        
/// <returns></returns>
        public static string GetScalar(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string result = null;
            DbCommand cmd 
= DBHelper.CreateCommand();
            cmd.CommandText 
= sql;
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                cmd.Connection.Open();
                result 
= cmd.ExecuteScalar().ToString();
            }
            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                result 
= (string)(cmd.ExecuteScalar());
            }
            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                cmd.Connection.Close();
            }
            
return result;
        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 返回数据表的第一行第一列,第二重重载,可执行存储过程
        
/// </summary>
        
/// <param name="sql"></param>
        
/// <param name="values"></param>
        
/// <returns></returns>
        public static string GetScalar(string sql, params DbParameter[] values)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string result = null;
            DbCommand cmd 
= null;
            cmd.CommandText 
= sql;
            cmd.CommandType 
= CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                cmd 
= DBHelper.CreateCommand();
                cmd.Connection.Open();
                result 
= cmd.ExecuteScalar().ToString();
            }
            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                result 
= (string)(cmd.ExecuteScalar());
            }
            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                cmd.Connection.Close();
            }
            
return result;
        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 把数据转换成XML形式输出,第一重重载
        
/// </summary>
        
/// <param name="sql"></param>
        
/// <returns></returns>
        public static string GetXmlReaderString(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            StringBuilder xmlString 
= new StringBuilder();
            SqlCommand sqlcmd 
= (SqlCommand)(DBHelper.CreateCommand());
            sqlcmd.CommandText 
= sql;
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                sqlcmd.Connection.Open();
                XmlReader reader 
= sqlcmd.ExecuteXmlReader();
                reader.Read();
                xmlString.Append(
"<root>");
                
while (!reader.EOF)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    xmlString.Append(reader.ReadOuterXml());
                }
                xmlString.Append(
"</root>");
            }
            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw ex;
            }
            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                sqlcmd.Connection.Close();
            }
            
return xmlString.ToString();
        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 把数据转换成XML形式输出,第二重重载,可执行存储过程
        
/// </summary>
        
/// <param name="sql"></param>
        
/// <param name="values"></param>
        
/// <returns></returns>
        public static string GetXmlReaderString(string sql,params SqlParameter[] values)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            StringBuilder xmlString 
= new StringBuilder();
            SqlCommand sqlcmd 
= (SqlCommand)(DBHelper.CreateCommand());
            sqlcmd.CommandText 
= sql;
            sqlcmd.CommandType 
= CommandType.StoredProcedure;
            sqlcmd.Parameters.AddRange(values);    
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                sqlcmd.Connection.Open();
                XmlReader reader 
= sqlcmd.ExecuteXmlReader();
                reader.Read();
                xmlString.Append(
"<root>");
                
while (!reader.EOF)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    xmlString.Append(reader.ReadOuterXml());
                }
                xmlString.Append(
"</root>");
            }
            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw ex;
            }
            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                sqlcmd.Connection.Close();
            }
            
return xmlString.ToString();
        }
    }
}
Model:
using
 System;
using
 System.Collections.Generic;
using
 System.Linq;
using
 System.Text;
namespace
 Model
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class User
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
string username;
        
string password;
        
public string Username
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return this.username; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set this.username = value; }
        }
        
public string Password
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return this.password; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set this.password = value; }
        }
    }
}
BLL:
using
 System;
using
 System.Collections.Generic;
using
 System.Linq;
using
 System.Text;
using
 Model;
using
 DAL;
using
 System.Data;
namespace
 BLL
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class BLLCheckUser
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public static string Check(User user)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string sql = "select * from tb_User where UserId='" + user.Username + "' and Password='" + user.Password + "'";
            DataTable table 
= DBHelper.ExecuteCommand(sql);
            
if (table.Rows.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if(table.Rows[0]["UserId"].ToString()!="")
                
return "true";
            }
            
return "false";
        }
    }
}

页面:

ExpandedBlockStart.gif
ContractedBlock.gif
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" 
%>
<!
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
>
ExpandedBlockStart.gifContractedBlock.gif
<
style 
type
="text/css"
>
ExpandedSubBlockStart.gifContractedSubBlock.gifbody
{
}
{
    text-align
: center;
    font-size
: 12px;
    color
:#666;
    font-family
: font-family: Arial, Helvetica, sans-serif;
}
table
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
}
{
    margin-top
: 160px;
    padding-top
: 30px;
    padding-bottom
: 30px;
    border
: solid 2px #333;
    background
: #fafcfd;
    
}
h1
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
}
{
    width
: 200px;
    font-size
:24px;
    text-align
: center;
    margin
: 0;
    padding
: 0;
    margin-left
: 100px;
    
}
ExpandedSubBlockStart.gifContractedSubBlock.giftd
{
}
{
    width
: 200px;
    height
: 30px;
    padding
: 6px;
}
.field
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
}
{
    width
: 120px;
    border
: solid 1px #ccc;
}
.login_btn
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
}
{
    border
: solid 1px #ccc;
    background
: #fafcfd;
    text-align
:center;
    width
: 60px;
    font-size
: 12px;
    color
: #666;
    margin
: 10px;
    padding-top
:2px;
    line-height
: 16px;
}
    
</
style
>
</
head
>
<
body
>
    
<
form 
action
="Office.aspx"
 method
="post"
>
        
<
table 
align
="center"
>
            
<
tr
>
                
<
td 
colspan
="2"
><
h1
>
Office办公自动化
</
h1
></
td
>
            
</
tr
>
            
<
tr
>
                
<
td 
align
="right"
>
用户名:
</
td
>
                
<
td 
align
="left"
><
input 
type
="text"
 size
="9"
 class
="field"
 id
="_name"
/></
td
>
            
</
tr
>
            
<
tr
>
                
<
td 
align
="right"
>
密 
&nbsp;
码:
</
td
>
                
<
td 
align
="left"
><
input 
type
="password"
 class
="field"
 id
="_pwd"
/></
td
>
            
</
tr
>
            
<
tr
>
                
<
td 
align
="right"
><
input 
type
="button"
 value
="登陆"
 class
="login_btn"
 onclick
="sendTo()"
/><
input 
type
="submit"
 id
="submit"
 style
="display:none"
/></
td
>
                
<
td 
align
="left"
><
input 
type
="reset"
value
="取消"
 class
="login_btn"
/></
td
>
            
</
tr
>
         
        
</
table
>
    
</
form
>
</
body
>
</
html
>

JS脚本:

function
 InitHttpRequest()
ExpandedBlockStart.gifContractedBlock.gif
{
    
var HttpRequest=null;
    
if(window.ActiveXObject)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try{
        HttpRequest
=new ActiveXObject("Msxml2.XMLHTTP");
        }
        
catch(e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try{
                HttpRequest
=new ActiveXObject("Microsofr.XMLHTTP");
            }
            
catch(e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
            }
        }
    }
    
else if(window.XMLHttpRequest)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        HttpRequest 
= new XMLHttpRequest();
    }
    
return HttpRequest;
}
function
 sendRequest(url,data)
ExpandedBlockStart.gifContractedBlock.gif
{
    
var Request = InitHttpRequest();
ExpandedSubBlockStart.gifContractedSubBlock.gif    Request.onreadystatechange 
= function() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (Request.readyState == 4 && Request.Status == 200{
            
var recieve = Request.responseText;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (recieve == "true"{
                document.getElementById(
"submit").click();
                
//alert("验证通过!");
            }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
else {
                alert(
"用户名或密码错误!");
            }
        }
    }
    Request.open(
"POST", url, false);
    Request.setRequestHeader(
"Content-Type""application/x-www-form-urlencoded");
    Request.send(data);
}
function
 sendTo()
ExpandedBlockStart.gifContractedBlock.gif
{
    
var url = "Ajax/CheckUser.aspx";
    
var post_str = "_name=" + document.getElementById("_name").value + "&_pwd=" + document.getElementById("_pwd").value;//构造POST参数
    sendRequest(url,post_str);
}

异步调用页面:

using
 System;
using
 System.Collections.Generic;
using
 System.Linq;
using
 System.Web;
using
 System.Web.UI;
using
 System.Web.UI.WebControls;
using
 Model;
using
 BLL;
public
 
partial
 
class
 AJAX_CheckUser : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
{
    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
string username = Request.Params["_name"];
        
string password = Request.Params["_pwd"];
        User user 
= new User();
        user.Username 
= username;
        user.Password 
= password;
        
string check = BLLCheckUser.Check(user);
        Response.Write(check);
    }
}

 

你可能感兴趣的文章
微软云创益大赛颁奖仪式现场
查看>>
DPM2012系列之十九:配置辅助备份服务器
查看>>
xmanager5连接CENTOS6
查看>>
《微软云计算Microsoft Azure部署与管理指南》即将上市!!!
查看>>
SpreadJS纯前端表格控件V11.1震撼来袭
查看>>
SIEM期待实时安全分析
查看>>
各位最近找我索要CCNA200-120的资源的同志些
查看>>
寻找***内网登陆外网的秘密通道
查看>>
iOS开发那些事--OCUnit测试框架
查看>>
foreman架构的引入7-Foreman结合mcollective完成push动作
查看>>
Tip:SCOM 2012 R2推送安装代理报错处理
查看>>
Drupal7配置之上传进度条
查看>>
《从零开始学Swift》学习笔记(Day 53)——do-try-catch错误处理模式
查看>>
一对一培训之视频免费分享-2018-05-03-SFB 2015-外部不能登录(排错笔记)
查看>>
“互联网+”:工业革命的需求与变革
查看>>
配置Lync Server 2010自动设置位置
查看>>
Centos 5.5 上面安装Open***完整版上篇【服务端配置】
查看>>
TCL微电影节、娱乐营销内容是王道
查看>>
Azure运维系列 6:使用自定义映像创建虚拟机
查看>>
如何对部署完成的Hyper-v 3.0群集进行验证
查看>>