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;

public partial class admin_Admin_Login : System.Web.UI.Page
{
    static string sValidator = "";
    private readonly string sValidatorImageUrl = "ValidateImage.aspx?Validator=";
    BLL.UserInfo user = new BLL.UserInfo();
    protected void Page_Load(object sender, EventArgs e)
    {
        Title = "系统登录";
        if (!Page.IsPostBack)
        {
            sValidator = GetRandomint();
            ValidateImage.ImageUrl = sValidatorImageUrl + sValidator;
        }
    }
    /// <summary>
    /// 产生随机数,范围100000~999999
    /// </summary>
    /// <returns></returns>
    private String GetRandomint()
    {
        Random random = new Random();
        return (random.Next(100000, 999999).ToString());
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            if (sValidator == Code.Text.ToString())
            {
                string pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(this.userpwd.Text.ToString().Trim(), "MD5");
                if (user.validateUser(this.username.Text.ToString().Trim(), pwd, 2))
                {
                    user.SetCookie(this.username.Text.ToString().Trim());
                    this.Response.Redirect("Admin_Main.aspx");
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('用户名和密码不一致!')</script>", false);
                }
            }
            else
            {
                Response.Write("<script>alert('验证码错误');location.href='Admin_Login.aspx';</script>");
            }
        }
    }
}
