namespace SK.MemberWeb.Templates.Default
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Web.Security;
	using SK.MemberBLL;

	/// <summary>
	///		cu_Login 的摘要说明。
	/// </summary>
	public class cu_Login : BaseControl
	{
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder3;
		protected System.Web.UI.WebControls.TextBox txtMemberName;
		protected System.Web.UI.WebControls.TextBox txtPwd;
		protected System.Web.UI.WebControls.TextBox txtVerifyCode;
		protected System.Web.UI.WebControls.Button btnLogin;
		protected SK.MemberWeb.Controls.ControlHolder ControlHolder1;
		MemberSystem member = new MemberSystem();

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			this.GiveFocus(txtMemberName);
			txtMemberName.Attributes["onkeypress"] = GetExecuteControlScript(btnLogin);
			txtPwd.Attributes["onkeypress"] = GetExecuteControlScript(btnLogin);
			txtVerifyCode.Attributes["onkeypress"] = this.GetExecuteControlScript(btnLogin);
			btnLogin.Attributes["onclick"] = "return checklogin();";
			
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		设计器支持所需的方法 - 不要使用代码编辑器
		///		修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnLogin_Click(object sender, System.EventArgs e)
		{
			if(Session["verifycode"]==null)
			{
				this.DisplayAlert("超时，验证码丢失，请刷新登录页");
				return;
			}
			if(txtVerifyCode.Text.ToUpper()!=Session["verifycode"].ToString().ToUpper())
			{
				this.DisplayAlert("验证码错误");
				return;
			}
			BasePape des = new BasePape();
			des.EncryptKey = "sorkednc";
			des.InputString = txtPwd.Text;
			des.DesEncrypt();
			string EncryptString = des.OutString;
			object MemberID    = member.Login(txtMemberName.Text,EncryptString);
			if(MemberID != DBNull.Value)
			{
				DataRow dr = member.Info(MemberID);
				if(Convert.ToBoolean(dr["IsDenied"]))
				{
					this.DisplayAlert("此帐号现在被管理员禁止登录，请与管理员联系");
					return;
				}
				if(Convert.ToInt16(Main.GetValue("NeedValidEmail"))==1 && !Convert.ToBoolean(dr["EmailHasValid"]))
				{
					Response.Redirect("/NeedValidEmail.aspx");
				}
				FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
					MemberID+";"+txtMemberName.Text,
					DateTime.Now,
					DateTime.Now.AddMinutes(30),
					true,
					dr["Roles"].ToString(),
					FormsAuthentication.FormsCookiePath);
				string hash = FormsAuthentication.Encrypt(ticket);
				HttpCookie cookie = new HttpCookie(
					FormsAuthentication.FormsCookieName, 
					hash); 
				cookie.Domain = this.Domain;

				if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

				Response.Cookies.Add(cookie);
				string returnUrl = Request.QueryString["ReturnUrl"];
				if (returnUrl == null) returnUrl = "/Users/";
				Response.Redirect(returnUrl);
			}
			else
			{
				//log.Add(null,"后台以用户名["+txtLoginID.Text+"]登录失败",WebLogType.Security);
				this.DisplayAlert("帐号或密码错误，请检查后重新登录！");
			}
		}
	}
}
