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 Spaces;

public partial class System_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Utils.IsInstalled())
        {
            SpacesContext.Current.Context.Response.Redirect("~/install/install.aspx");
            return;
        }
        if (!this.IsPostBack)
            BindImg();
    }

    void BindImg()
    {
        //btnLogin.Text = Globals.Text["Login_loginButton"];

      
        PersistentCookie.Text = Globals.Text["Login_autoLogin"];
        if (Context.User.Identity.Name != null && Context.User.Identity.Name != string.Empty)
        {
            //Response.Redirect(string.Format("{0}/Admin",Globals.ApplicationPath));
            Account.Text = this.User.Identity.Name;
            PersistentCookie.Checked = true;

        }
      //  this.Image1.ImageUrl = string.Format("{0}/Include/VerifyCode.aspx", Globals.ApplicationPath);

    }
  

    public void SetTicket(string name, bool isPersistent)
    {
        FormsAuthentication.SetAuthCookie(name, isPersistent);
        
        //  this.Literal1.Text = GetCurrentUserName + "" + Page.User.Identity.Name;
        string userData = "username|userid|userdepart";

        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
        1,
        name,
        System.DateTime.Now,
        System.DateTime.Now.AddDays(7),
        isPersistent,
        userData,
        FormsAuthentication.FormsCookiePath);
      
        // Encrypt the ticket. 
        string encTicket = FormsAuthentication.Encrypt(ticket);
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
        cookie.Domain = "bfor.cn";
        // Create the cookie. 
      
        Response.Cookies.Add(cookie);
       /* HttpCookie user = new HttpCookie("CommentUser");
        user.Values["Name"] = Server.UrlEncode(name);
        user.Values["Url"] = Server.UrlEncode(string.Format(Config.GetConfig().DefaultDomain,name));
        user.Domain = "/";
        user.Expires = DateTime.Now.AddDays(30);
        Response.Cookies.Add(user);
        */
       // FormsAuthentication.RedirectFromLoginPage(name, isPersistent);
    }
    public static string GetCurrentUserName
    {
        get
        {
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                try
                {
                    return HttpContext.Current.User.Identity.Name;
                }
                catch { }
            }
            return null;
        }
    }
    protected void ibtnLogin_Click(object sender, ImageClickEventArgs e)
    {
       
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string username = Account.Text.ToLower();
        string password = Password.Text;
        string code = verifyCode.Text;

        if (Session["Verify"] == null || Session["Verify"].ToString() != code)
        {

            string script = "<script>alert('请输入正确的验证码！');history.go(-1);</script>";
            this.RegisterStartupScript("verifyErr", script);
            return;
        }
        
       
        password = Utils.PasswordFormat(password).ToLower();
        Spaces.User user = Users.GetUser(username);
        if (user == null)
        {
            string script = "<script>alert('用户名不存在！');history.go(-1);</script>";
            this.RegisterStartupScript("accountErr", script);
            return;
        }
       
        if (user.Password.ToLower() != password)
        {

            string script = "<script>alert('密码错误！');history.go(-1);</script>";
            this.RegisterStartupScript("passwordErr", script);
            return;
        }
        if (SiteTools.IsLoginDisallowed(username) || SiteTools.IsLoginDisallowed(user.Account))
        {
            SpacesContext.RedirectToMessage(MessageType.LoginBanned);
        }

        SetTicket(user.Account, PersistentCookie.Checked);
        user.LastIP = Request.UserHostAddress;
        user.DateLastLogin = DateTime.Now;
        Users.UpdateUser(user);
        string url = FormsAuthentication.GetRedirectUrl(user.Account, false);
        if (url.ToLower().IndexOf("/admin/") != -1 && url.ToLower().IndexOf("main.aspx") == -1)
            Response.Redirect(Globals.ApplicationPath + "/admin/main.aspx?url=" + url);
        else
            Response.Redirect(string.Format("{0}/Admin", Globals.ApplicationPath));
    }
}
