///验证码点击变化
<script type="text/javascript">
//实现验证码的点击变化
$(function () {
$("#Image1").css("cursor", "pointer").click(function () {
//让每一次验证码请求的地址不一样就可以了
var str = new Date().getMilliseconds();
$(this).attr("src", "ValidateCode.ashx?id=" + str);
});
});
</script>
///用户登录界面代码
public partial class Login : System.Web.UI.Page
{
public string UserName { get; set; }
public string AlertMsg { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)//用户提交过来的
{
#region 保存用户名
//获取用户输入的用户名
string userEnterName = Request["txtClientID"];
UserName = userEnterName;
//为防止出现乱码将用户名使用Url编码
userEnterName = Server.UrlEncode(userEnterName);
//将用户名通过cookie写入浏览器端
Response.Cookies["userName"].Value = userEnterName;
//设置cookie的过期时间。两天
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(2);
#endregion
#region 校验验证码
//获取用户输入的验证码
string userEnterVcode = Request["txtCode"];
//系统生成的验证码
string vCode = Session["vCode"] as string;
if (userEnterVcode != vCode)
{
//返回一段jq代码
AlertMsg = "<script type='text/javascript'>$(function(){alert('验证码输入错误')}) </script>";
}
else
{
#region 校验用户名和密码
string name=Request["txtClientID"];
string pwd= Request["txtPassword"];
BLL.HKSJ_USERS user = new BLL.HKSJ_USERS();
string strWhere=string.Format("LoginName='{0}' and PassWord='{1}'",name,pwd);
List<Model.HKSJ_USERS> list= user.GetModelList(strWhere);
if (list.Count > 0)
{
Session["Login"] = list[0];
Response.Redirect("/20160310/AdminMainWebForm.aspx");
}
else
{
AlertMsg = "<script type='text/javascript'>$(function(){alert('用户名或密码错误')}) </script>";
}
#endregion
}
#endregion
}
else//用户第一次请求
{
//将用户的登录名从cookie中取出来,当没有的话为空
string strCookiesValue = Request["userName"];
//解码后填入到前台页面中去
UserName = Server.UrlDecode(strCookiesValue);
}
}
}
///验证码获取代码
public void ProcessRequest(HttpContext context)
{
Common.ValidateCode validateCode = new Common.ValidateCode();
string vCode= validateCode.CreateValidateCode(4);
//将验证码存储到session中
context.Session["vCode"] = vCode;
validateCode.CreateValidateGraphic(vCode, context);
}
///基类的代码
///当当前页面不能通过在地址栏输入直接获取的时候可以让页面类继承这个基类
public class BasePage:System.Web.UI.Page
{
protected virtual void Page_Init(object sender, EventArgs e)
{
if (Session["Login"] == null)
{
Response.Redirect("/2016年3月16日/LoginDemo/Login.aspx");
}
}
}