Ejemplo n.º 1
0
 /// <summary>
 /// 写入日志
 /// </summary>
 /// <param name="action">动作</param>
 public static void WriteLogActions(string action)
 {
     if (IsAdminLogin())
     {
         string adminLogId = SessionHelper.GetSession(sessionAdminLogIDKey).ToString();
         //string adminLogId = AuthenticationHelper.GetClaim(sessionAdminLogIDKey);
         if (string.IsNullOrEmpty(adminLogId))
         {
             adminLogId = CookiesHelper.GetCookie(cookiesAdminLogIDKey);//日志GUID
             //adminLogId = AuthenticationHelper.GetClaim(cookiesAdminLogIDKey);//日志GUID
         }
         if (!string.IsNullOrEmpty(adminLogId))
         {
             AdminLog log = AdminLog.FindByGUID(adminLogId);
             if (log != null)
             {
                 if (string.IsNullOrEmpty(log.Actions))
                 {
                     log.Actions = $"{DateTime.Now:yyyy-MM-dd HH:mm}: {action}";
                 }
                 else
                 {
                     log.Actions = log.Actions + $"|||{DateTime.Now:yyyy-MM-dd HH:mm}: {action}";
                 }
                 log.LastUpdateTime = DateTime.Now;
                 log.Update();
             }
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 验证管理员是否登录
        /// </summary>
        /// <returns>是否登录</returns>
        public static bool IsAdminLogin()
        {
            string adminName = SessionHelper.GetSession(sessionAdminNameKey).ToString(); //用户名
            string adminID   = SessionHelper.GetSession(sessionAdminIDKey).ToString();   //ID

            //如果Session失效,则用Cookies判断
            if (string.IsNullOrEmpty(adminName) || string.IsNullOrEmpty(adminID))
            {
                string cooAdminName  = CookiesHelper.GetCookie(cookiesAdminNameKey);  //用户名
                string cooAdminID    = CookiesHelper.GetCookie(cookiesAdminIDKey);    //ID
                string cooLoginInfo  = CookiesHelper.GetCookie(cookiesAdminInfoKey);  //信息
                string cooAdminLogID = CookiesHelper.GetCookie(cookiesAdminLogIDKey); //日志GUID

                if (string.IsNullOrEmpty(cooAdminID) || string.IsNullOrEmpty(cooAdminName) || string.IsNullOrEmpty(cooLoginInfo) || string.IsNullOrEmpty(cooAdminLogID))
                {
                    return(false);//信息不完整
                }
                else
                {
                    //全不为空则判断信息是否正确
                    Admin model = Find(Admin._.UserName == Utils.SqlStr(cooAdminName));// FindByName(Utils.SqlStr(cooAdminName));
                    if (model == null)
                    {
                        return(false);//找不到管理员
                    }
                    else
                    {
                        if (Utils.MD5(model.UserName + model.PassWord + model.Salt + Utils.GetIP()) == cooLoginInfo)
                        {
                            //信息正确,重建session
                            //获取日志ID
                            if (AdminLog.FindByGUID(cooAdminLogID) == null)
                            {
                                ClearInfo();   //清除信息
                                return(false); //日志出错
                            }

                            //重新写入Session 和 Cookies
                            SetAdminInfo(model.UserName, model.PassWord, model.Id, 0, "", cooAdminLogID, model.Salt);
                            return(true);
                        }
                        else
                        {
                            ClearInfo();   //清除信息
                            return(false); //信息错误
                        }
                    }
                }
            }
            else
            {
                return(true);//Session未失效,正确
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 后台管理员登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        /// <returns>是否登录成功</returns>
        public static bool AdminLogin(String userName, String passWord)
        {
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
            {
                return(false);
            }

            userName = Utils.SqlStr(userName.Trim());
            Guid     GUID   = System.Guid.NewGuid();
            Admin    entity = Find(Admin._.UserName == userName);
            AdminLog log    = new AdminLog();

            log.GUID           = GUID.ToString();
            log.IsLoginOK      = 0;
            log.PassWord       = passWord.Trim();
            log.LoginIP        = Utils.GetIP();
            log.LoginTime      = DateTime.Now;
            log.UserName       = userName.Trim();
            log.LastUpdateTime = DateTime.Now;

            if (entity == null)
            {
                log.Actions = "登录失败:用户名错误。";
                log.Insert();
                return(false);
            }
            else
            {
                if (entity.PassWord != Utils.MD5(entity.Salt + passWord.Trim()))
                {
                    log.Actions = "登录失败:密码错误。";
                    log.Insert();
                    return(false);
                }
                else
                {
                    entity.LastLoginTime = DateTime.Now;
                    entity.Update();
                    //添加到记录
                    log.IsLoginOK = 1;
                    log.PassWord  = "******";
                    log.Insert();
                    //写入Session 和 Cookies
                    //SessionHelper.WriteSession("rtadminguid", GUID.ToString());
                    //CookiesHelper.WriteCookie("rtadminguid", GUID.ToString(), 120);
                    SetAdminInfo(entity.UserName, entity.PassWord, entity.Id, 0, "", GUID.ToString(), entity.Salt);
                    //SetAdminInfoAsync(entity.UserName, entity.PassWord, entity.Id, 0, "", GUID.ToString(), entity.Salt).Wait();

                    return(true);
                }
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 写入日志
 /// </summary>
 /// <param name="action">动作</param>
 public static void WriteLogActions(string action)
 {
     if (IsAdminLogin())
     {
         string adminLogId = SessionHelper.GetSession(sessionAdminLogIDKey).ToString();
         if (string.IsNullOrEmpty(adminLogId))
         {
             adminLogId = CookiesHelper.GetCookie(cookiesAdminLogIDKey);//日志GUID
         }
         if (!string.IsNullOrEmpty(adminLogId))
         {
             AdminLog log = AdminLog.FindByGUID(adminLogId);
             if (log != null)
             {
                 log.Actions        = log.Actions + action;
                 log.LastUpdateTime = DateTime.Now;
                 log.Update();
             }
         }
     }
 }