<%@ Page Language="C#" AutoEventWireup="true" Inherits="Ycbw.Data.OleDbClient" %>

<script language="c#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
	if (!Page.IsPostBack)
	{
		try
		{
			this.text_username.Text = this.GetCookie("UserInfo", "username");

			this.text_password.Text = this.GetCookie("UserInfo", "password");
			this.check_password.Checked = true;

			this.CheckUser(this.text_username.Text, this.text_password.Text);
		}
		catch
		{

		}
	}
}
 
protected void button_login_click(object sender, EventArgs e)
{
	this.CheckUser(this.text_username.Text, this.text_password.Text);
}

private void CheckUser(string username, string password)
{
	password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower();

	this.Fill("Select Top 1 * From [b_admin] Where username = '" + username + "' and password = '" + password + "' and adminid > 0", "data_user");

	if (this.Data.Tables["data_user"].Rows.Count == 1)
	{
		System.Data.DataRow dr = this.Data.Tables["data_user"].Rows[0];

		//再次比对用户名和密码
		if( dr["username"].ToString() == username && dr["password"].ToString() == password )
		{
			//记住用户名和密码
			System.Web.HttpCookie cookie = new HttpCookie("UserInfo");
	
			if (this.check_password.Checked)
			{
				this.SetCookie(cookie, "username", username);
				this.SetCookie(cookie, "password", this.text_password.Text);
			}
	
			//设置用户为登陆状态
			Session["admin"]     = dr["username"].ToString();
			Session["adminname"] = dr["nickname"].ToString();
			Session["adminid"]   = dr["adminid"].ToString();
			Session["purview"]   = dr["purview"].ToString();
	
			//this.Execute( "Insert Into crm_Log (username, action, field) Values ( '"+ Session["username"].ToString() +"' , 'Login', "+ Session["field"] +")" );
	
			Response.Redirect("index.aspx");
		}
		else
		{
			this.text_error.Text = "用户名或密码错误";
		}
	}
	else
	{
		this.text_error.Text = "用户名或密码错误";
	}
}
</script>

<html>
<head>
<title><%= this.Configuration["title"].ToString() %></title>
<link href="css/default.css" rel="stylesheet" type="text/css">
<link href="resources/css/ext-all.css" rel="stylesheet" type="text/css">
</head>
<body>
<form id="form1" runat="server">
  <table width="778" border="0" align="center" cellpadding="0" cellspacing="10">
    <tr>
      <td width="100" height="70"><a href="/"><img src="/images/logo.jpg" border="0"></a></td>
      <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="35" bgcolor="#eaf3ff">&nbsp;</td>
        </tr>
      </table></td>
    </tr>
  </table>
  <table width="778" border="0" align="center" cellpadding="10" cellspacing="0" background="images/bg_login.jpg">
    <tr>
      <td align="right">
	  <table width="100%" border="0" cellpadding="8" cellspacing="0">
        <tr>
          <td width="400" align="center" valign="top" style="padding:0; padding-right:20px;"><img src="images/index.jpg" width="420" height="280"></td>
          <td align="center" bgcolor="#edf5ff" style="border:1px solid #acc3e4;"><table border="0" cellpadding="0" cellspacing="10" class="font14">
        <tr>
          <td colspan="2" class="title1">登录系统</td>
          </tr>
        <tr>
          <td>用户名：</td>
          <td width="200"><asp:TextBox ID="text_username" runat="server" CssClass="text2" onkeypress="return text_username_keyPress(event)" style="width:200px; height:30px; padding-top:5px;" /></td>
        </tr>
        <tr>
          <td>密码：</td>
          <td><asp:TextBox ID="text_password" TextMode="Password" CssClass="text2" runat="server" style="width:200px; height:30px; padding-top:5px;" /><br>
          <asp:Label ID="text_error" ForeColor="#FF0000" runat="server" /></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>
            <asp:CheckBox ID="check_password" Text="记住密码" runat="server" />            
          </td>
        </tr>
        <tr>
          <td></td>
          <td><asp:Button Text=" 登陆 " Width="60" Height="25" OnClick="button_login_click" runat="server" /></td>
        </tr>
      </table></td>
        </tr>
      </table></td>
    </tr>
  </table>
  <table width="778" border="0" align="center" cellpadding="0" cellspacing="10">
    <tr>
      <td height="70"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="35" align="center" bgcolor="#eaf3ff"> 2008 - <%= DateTime.Now.Year.ToString() %> 宜昌博网科技有限公司 All Rights Reserved</td>
          </tr>
      </table></td>
    </tr>
  </table>
  <script language="javascript">
function text_username_keyPress( e )
{
	var k = event.keyCode; 

	if( parseInt(k) == 13 )
	{
		document.getElementById("text_password").focus();

		return false;
	}
	
	return true;
}

window.onload = function()
{
	document.form1.text_username.focus();
}
</script>

</form>
</body>
</html>
