using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


public partial class Admin_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string loginstring = Request.QueryString["Out"];
            if (loginstring != null && loginstring == "loginout")
            {
                Session["BookName"] = "";
                Response.Redirect("Login.aspx");

            }

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string Username = this.UserName.Text.ToString();
        string password = this.Password.Text.ToString();
        string mycode = this.myCode.Text.ToString();
        string loginIP = Request.UserHostAddress;



        if (Username == "" || Username == null)
        {
            Response.Write("<script>alert('用户名不能为空！');</script>");
            return;
        }
        if (password == "" || password == null)
        {
            Response.Write("<script>alert('密码不能为空！');</script>");
            return;
        }
        if (!mycode.Equals(Session["code"]))
        {
            Response.Write("<script>alert('验证码错误！');</script>");
            return;
        }

        string sql = "select * from A_Admin where Name='" + Username + "'";
        DataSet dr;
        if (ISDatabase.SelectDatebase() == 1)
            dr = DbHelp.Query(sql);
        else
            dr = DbHelperOleDb.Query(sql);

        if (dr.Tables[0].Rows.Count > 0)
        {
            string md5Name = PubliclClass.MD5(password);


            if (md5Name.Equals(dr.Tables[0].Rows[0]["Password"].ToString()))
            {

                string sql1 = "update A_Admin set Loginip='" + loginIP + "' where Name='" + Username + "'";
                if (ISDatabase.SelectDatebase() == 1)
                    DbHelp.ExecuteSql(sql1);
                else
                    DbHelperOleDb.ExecuteSql(sql1);

                Session["BookName"] = dr.Tables[0].Rows[0]["Id"].ToString();
                Session["LoginName"] = dr.Tables[0].Rows[0]["Name"].ToString();
                Response.Write("<script>alert('登录成功！'); window.location.href='mainframe/index.aspx';</script>");
                string CreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                string sqlloginlog = string.Empty;
                if (ISDatabase.SelectDatebase() == 1)
                {
                    sqlloginlog = "insert into A_System_login_log(CreateDate,LoginName,Status,IPAddress,IPAddressName) values('" + CreateDate + "','" + Username + "','登陆成功','" + loginIP + "','后台登陆')";
                    DbHelp.ExecuteSql(sqlloginlog);
                }
                else
                {
                    sqlloginlog = "insert into A_System_login_log(CreateDate,LoginName,Status,IPAddress,IPAddressName) values(#" + CreateDate + "#,'" + Username + "','登陆成功','" + loginIP + "','后台登陆')";
                    DbHelperOleDb.ExecuteSql(sqlloginlog);
                }

                return;

            }
            else
            {
                string CreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                string sqlloginlog = string.Empty;
                if (ISDatabase.SelectDatebase() == 1)
                {
                    sqlloginlog = "insert into A_System_login_log(CreateDate,LoginName,Status,IPAddress,IPAddressName) values('" + CreateDate + "','" + Username + "','登陆失败密码错误','" + loginIP + "','后台登陆')";
                    DbHelp.ExecuteSql(sqlloginlog);
                }
                else
                {
                    sqlloginlog = "insert into A_System_login_log(CreateDate,LoginName,Status,IPAddress,IPAddressName) values(#" + CreateDate + "#,'" + Username + "','登陆失败密码错误','" + loginIP + "','后台登陆')";
                    DbHelperOleDb.ExecuteSql(sqlloginlog);
                }
                Response.Write("<script>alert('密码错误！'); </script>");
                return;
            }
        }
        else
        {
            Response.Write("<script>alert('用户名错误！');</script>");
            string CreateDate = DateTime.Now.ToString("yyyy-MM-dd");
            string sqlloginlog = string.Empty;
            if (ISDatabase.SelectDatebase() == 1)
            {
                sqlloginlog = "insert into A_System_login_log(CreateDate,LoginName,Status,IPAddress,IPAddressName) values('" + CreateDate + "','" + Username + "','登陆失败用户名错误','" + loginIP + "','后台登陆')";
                DbHelp.ExecuteSql(sqlloginlog);
            }
            else
            {
                sqlloginlog = "insert into A_System_login_log(CreateDate,LoginName,Status,IPAddress,IPAddressName) values(#" + CreateDate + "#,'" + Username + "','登陆失败用户名错误','" + loginIP + "','后台登陆')";
                DbHelperOleDb.ExecuteSql(sqlloginlog);
            }
            return;
        }
    }
}
//author:Angel_asp
//QQ：709047174
//website:www.angelasp.com