using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using wormstrdo;

public partial class cuslogin : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = "SkinFile";
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void send_Click(object sender, EventArgs e)
    {
        int ok = 0;
        SqlConnection one = conn.sqlconn();
        if (Page.IsValid)
        {
            if (this.tbck.Text == Request.Cookies["vCode"].Value)
            {
                try
                {
                    one.Open();
                    string strcname = cname.Text;
                    string strcpw = cpw.Text;
                    strreplace str = new strreplace();
                    strcname = str.txtstr(strcname);
                    strcpw = str.txtstr(strcpw);

                    SqlCommand cmd = new SqlCommand("select * from customer where cname='" + strcname + "' and cpw='" + strcpw + "'", one);  //建立SQL命令
                    SqlDataReader rs = cmd.ExecuteReader();
                    if (rs.Read())
                    {

                        Session["ad"] = strcname;
                        Session["pw"] = strcpw;
                        Session["profile"] = rs["clei"].ToString();
                        //写入客户的cookie
                        cookie mycookie = new cookie();
                        if (save_cook.Checked)
                        {
                            mycookie.wr_cookie("wormcook", "ad", strcname, "pw", strcpw,"file", rs["clei"].ToString(), 90);
                        }
                        //这是用户登录，更改Application["EveryIP"]的用户名
                        string newad;
                        string oldad;
                        if (Application["EveryIP"].ToString().IndexOf(Request.UserHostAddress.ToString()) >= 0)
                        {
                            if (Session["ad"].ToString() == "")
                            { newad = "游客"; }
                            else
                            { newad = Session["ad"].ToString(); }
                            if (Session["oldad"].ToString() == "")
                            { oldad = "游客"; }
                            else
                            { oldad = Session["oldad"].ToString(); }
                            string tempip = oldad + "[" + Session["ip"].ToString() + "]&nbsp;&nbsp;|&nbsp;&nbsp;";    //登录前用户
                            string tempip2 = newad + "[" + Session["ip"].ToString() + "]&nbsp;&nbsp;|&nbsp;&nbsp;";      //登录后用户
                            Application.Lock();
                            Application["EveryIP"] = Application["EveryIP"].ToString().Replace(tempip, tempip2);
                            Application.UnLock();//解锁
                        }
                        else
                        {
                            if (Session["ad"].ToString() == "")
                            { newad = "游客"; }
                            else
                            { newad = Session["oldad"].ToString(); }
                            string tempip = newad + "[" + Session["ip"].ToString() + "]&nbsp;&nbsp;|&nbsp;&nbsp;";      //登录后用户
                            Application["EveryIP"] = Application["EveryIP"] + tempip;
                            Application.UnLock();//解锁
                        }
                        //End
                        Session["oldad"] = Session["ad"].ToString();
                        ok = 1;
                    }
                    rs.Close();
                    if (ok == 1)
                    {
                        string Sql = "update customer set cdeng=cdeng+1 where cname='" + Convert.ToString(Session["ad"]) + "'";
                        cmd = new SqlCommand(Sql, one);
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                        Response.Write("<script>window.parent.alert('用户登录成功！');window.parent.location='default.aspx';</script>");
                    }
                    else
                    {
                        Response.Write("<script>window.alert('用户登录失败！')</script>");
                    }
                }
                catch (System.Exception ee)
                {
                    Response.Write(ee.Message.ToString());
                }
                finally
                {
                    one.Close();
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('验证码错误,请重新输入！');</script>");
            }
        }
    }
    protected void reset_Click(object sender, EventArgs e)
    {
        cname.Text = "";
        cpw.Text = "";
    }
}
