Login.aspx前台页面:
<script type="text/javascript">
function changecode() {
var img = document.getElementById("<%=imgCode.ClientID%>");
img.src = "CheckCode.aspx?" + Math.random(1);
return;
}
方法一:使用Jquery
$(function () {
//$("#btn").click = function submitFrm() {
$("#btn").click(function () {
var txtName = $("#TxtUserName").val();
var txtPwd = $("#TxtPassword").val();
var txtCheckCode = $("#txtValidateCode").val();
if (txtName == "" || txtPwd == "") {
alert("用户名或密码不能为空,请重新输入");
return;
}
//if (txtCheckCode == "") {
// alert("验证码不能为空");
// return;
//}
$.post("PageLogin.ashx", { name: txtName, pwd: txtPwd, checkCode: txtCheckCode }, function (date) {
if (date == "OK") {
location.href = "Default.aspx";
} else {
alert(date);
return;
}
});
});
});
方法二:使用纯Js:
<!--<script type="text/javascript">
window.onload = function () {
var btnLogin = document.getElementById("btn");
btnLogin.onclick = function () {
var txtName = document.getElementById("TxtUserName").value;
var txtPwd = document.getElementById("TxtPassword").value;
var txtCheckCode = document.getElementById("txtValidateCode").value;
if (txtName== "" || txtPwd == "") {
alert("用户名或密码不能为空,请重新输入");
return;
}
if (txtCheckCode == "") {
alert("验证码不能为空");
return;
}
var strUrl = "PageLogin.ashx?name=" + txtName + "&pwd=" + txtPwd + "&checkCode=" + txtCheckCode;
myAjax("get", strUrl, function (data) {
if (data == "OK") {
window.location.href = "Default.aspx";
} else {
alert(data);
}
});
};
};
function myAjax(httpMethod, url, callback) {
//发送异步请求
var xhr;
if (XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open(httpMethod, url, true);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
callback(xhr.responseText);
}
};
}
</script>-->
</script>
Login.aspx后台页面:
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.TxtUserName.Focus();
}
}
PageLogin.ashx:
public class PageDelete : IHttpHandler, IReadOnlySessionState
{
BLL.tb_User bll = new BLL.tb_User();
Model.tb_User model = new Model.tb_User();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string err = "";
string name = context.Request["name"].ToLower();
string pass = BasePage.Encrypt(context.Request["pwd"].ToLower(), "Domaingz");
string checkCode = context.Request["checkCode"].ToLower();
//if (context.Session["CheckCode"] != null)
//{
// if (context.Session["CheckCode"].ToString().ToLower() != checkCode)
// {
// err = "输入验证码不正确,请重新输入!";
// context.Response.Write(err);
// return;
// }
//}
//else
//{
// err = "请重新输入验证码!";
// context.Response.Write(err);
// return;
//}
if (name.IndexOf("'") != -1 || pass.IndexOf("'") != -1)
{
err = "对不起,请不要包含特殊字符!";
context.Response.Write(err);
return;
}
if (name != "" && pass != "")
{
var dt = bll.GetList(" UserCode='" + context.Request["name"] + "' AND Password='" + BasePage.Encrypt(context.Request["pwd"], "Domaingz") + "' AND IsDelete=0").Tables[0];
if (System.Web.HttpContext.Current.Session["UserCode"] != null)
{
System.Web.HttpContext.Current.Session.Remove("UserID");
System.Web.HttpContext.Current.Session.Remove("UserCode");
System.Web.HttpContext.Current.Session.Remove("UserName");
}
if (dt.Rows.Count > 0)
{
System.Web.HttpContext.Current.Session.Add("UserID", dt.Rows[0]["ID"]);
System.Web.HttpContext.Current.Session.Add("UserCode", dt.Rows[0]["UserCode"]);
System.Web.HttpContext.Current.Session.Add("UserName", dt.Rows[0]["UserName"]);
InsertLog("用户登录,用户:" + name + "在[" + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + "]登录;登录IP:" + context.Request.UserHostAddress);
context.Response.Write("OK");
}
else
{
err = "密码或者用户名不正确,请重新输入";
context.Response.Write(err);
return;
}
}
}
public void InsertLog(string strPageContent)
{
BLL.tb_Log logBll = new BLL.tb_Log();
Model.tb_Log logModel = new Model.tb_Log();
logModel.CreataDate = DateTime.Now;
logModel.LogType = 1;//记录日志
logModel.PageContent = strPageContent;
//logModel.LogContent = strErro;
logModel.UserID = Convert.ToInt32(System.Web.HttpContext.Current.Session["UserID"]);
logBll.Add(logModel);
}
public bool IsReusable
{
get
{
return false;
}
}
}