/// <summary>
        /// 登入驗證,若成功後,將登入資訊存入Cookie
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password">未加密過的密碼</param>
        /// <returns></returns>
        public override bool Login(string account, string password)
        {
            //取得帳號資料
            var userDetail      = User.FetchAdminUserDetail(account);
            var encryptPassword = Md5.Encrypt(password);

            //檢查使用者的密碼
            if (string.IsNullOrEmpty(userDetail.EncryptPassword) || !userDetail.EncryptPassword.Equals(encryptPassword))
            {
                return(false);
            }

            var principal = new ManagerPrincipal(userDetail.Account);

            //HttpContext.Current.User = principal;
            //Identity = HttpContext.Current.User.Identity as ManagerIdentity;
            //註冊Cookies
            //AddCookie(userDetail);
            AddCookie(principal);
            CacheProvider.Insert(userDetail.Account, principal);

            //System.Diagnostics.Debug.WriteLine(CurrentUser);
            //更新最後登入時間及IP
            ////userDetail.CurrentLoginIp = Request.ServerVariables["REMOTE_ADDR"];
            //dataProvider.UpdateAdminUserLastLogin(userDetail);
            return(true);
        }
        public bool Login(UserParameter param)
        {
            //取得帳號資料
            var userDetail = User.FetchAdminUserDetail(param.User.Account);

            if (userDetail.Status == 2)
            {
                return(false);
            }

            var encryptPassword = Md5.Encrypt(param.User.Password);

            //檢查使用者的密碼
            if (string.IsNullOrEmpty(userDetail.EncryptPassword) || !userDetail.EncryptPassword.Equals(encryptPassword))
            {
                return(false);
            }
            //如果是最高管理者需要再驗證IP
            if (userDetail.IsAdministrator)
            {
                if (!VerifyConnectSourceIp(param.RemoteIp))
                {
                    return(false);
                }
            }
            var principal = new ManagerPrincipal(userDetail.Account);

            //HttpContext.Current.User = principal;
            //Identity = HttpContext.Current.User.Identity as ManagerIdentity;
            //註冊Cookies
            //AddCookie(userDetail);
            AddCookie(principal);
            CacheProvider.Insert(userDetail.Account, principal);
            return(true);
        }
        public new static void Logout()
        {
            CacheProvider.Clear(HttpContext.Current.User.Identity.Name);
            //var cookiesCount = HttpContext.Current.Request.Cookies.Count;
            //var cookies = new string[cookiesCount];
            //for (var i = 0; i < cookiesCount; i++) {
            //    Cookie.Set(
            //        new HttpCookie(HttpContext.Current.Request.Cookies[0].Name, "") {
            //            Domain = Section.Get.Web.Domain,
            //            //Expires = DateTime.MinValue
            //            Expires = DateTime.Now.AddSeconds(-Section.Get.Web.CookieTimeout)
            //        });
            //    // cookies[i] = HttpContext.Current.Request.Cookies[0].Name;
            //    // var cookie = new HttpCookie(cookies[i]);
            //    //// HttpCookie myCookie =
            //    // //if (null == cookie) return;
            //    // HttpContext.Current.Response.Cookies.Remove(cookie.Name);
            //    // cookie.Value = string.Empty;
            //    // cookie.Domain = Section.Get.Web.Domain;
            //    // cookie.Expires = DateTime.UtcNow.AddDays(-2d);
            //    // HttpContext.Current.Response.Cookies.Add(cookie);

            //    // Cookie.Set(
            //    //new HttpCookie(HttpContext.Current.Request.Url.Authority,"")
            //    //{
            //    //    Domain = Section.Get.Web.Domain,
            //    //     //Expires = DateTime.MinValue
            //    //     Expires = DateTime.Now.AddSeconds(-Section.Get.Web.CookieTimeout)
            //    //});
            //}
            //Cookie.Set(
            //    new HttpCookie(HttpContext.Current.Request.Url.Authority, "") {
            //        Domain = Section.Get.Web.Domain,
            //        //Expires = DateTime.MinValue
            //        Expires = DateTime.Now.AddSeconds(-Section.Get.Web.CookieTimeout)
            //    });

            //FormsAuthentication.SignOut();
            Cookie.Clear();
        }
 /// <summary>
 /// 從Cookie 中取得使用者資料
 /// </summary>
 /// <returns></returns>
 public override ManagerCurrentUser GetCookie()
 {
     if (!Cookie.IsExist(HttpContext.Current.Request.Url.Authority))
     {
         return(null);
     }
     try
     {
         // var ticket = FormsAuthentication.Decrypt(Cookie.Get(DbName.AgmOfficial));
         var infoString = AzDG.Decrypt(Cookie.Get(HttpContext.Current.Request.Url.Authority));
         var info       = infoString.Split(new[] { CookieSplittor }, StringSplitOptions.None);
         //20131203 加入快取機制
         var principal = CacheProvider.Get <IPrincipal>(info[1]);
         return(principal != null
             ? ((ManagerIdentity)(principal).Identity).CurrentUser
             : User.FetchAdminUserDetail(info[1]));
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
        /// <summary>
        /// 從 Cookie 或快取中取得使用者資料
        /// </summary>
        /// <returns></returns>
        public static IPrincipal GetUser()
        {
            if (!Cookie.IsExist(HttpContext.Current.Request.Url.Authority))
            {
                return(null);
            }

            if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
            {
                //return CacheProvider.Get<IPrincipal>(HttpContext.Current.User.Identity.Name);
                //重設過期時間
                AddCookie(HttpContext.Current.User);
                CacheProvider.Insert(HttpContext.Current.User.Identity.Name, HttpContext.Current.User);
                return(HttpContext.Current.User);
            }
            try
            {
                var infoString = AzDG.Decrypt(Cookie.Get(HttpContext.Current.Request.Url.Authority));
                var info       = infoString.Split(new[] { CookieSplittor }, StringSplitOptions.None);
                //20131203 加入快取機制
                var principal = CacheProvider.Get <IPrincipal>(info[1]);
                if (principal != null)
                {
                    AddCookie(principal);
                    CacheProvider.Insert(principal.Identity.Name, principal);
                    return(principal);
                }
                principal = new ManagerPrincipal(info[1]);
                //重設過期時間
                AddCookie(principal);
                CacheProvider.Insert(info[1], principal);
                return(principal);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }