/// <summary>
        /// 登录系统
        /// </summary>
        /// <returns></returns>
        public JsonResult LoginSystem()
        {
            string loginUsername = string.IsNullOrEmpty(Request["loginUsername"]) ? "" : Request["loginUsername"];
            string loginPassword = string.IsNullOrEmpty(Request["loginPassword"]) ? "" : Request["loginPassword"];

            if (loginUsername == "" || loginPassword == "")
            {
                return(Json(new { result = false, msg = "未获取到登录信息,请联系管理员处理!" }));
            }
            var userInfo = userManageLogic.QueryUserByPassword(new UseManageRequest
            {
                UserName = loginUsername,
                PassWord = loginPassword
            });

            if (userInfo != null)
            {
                //将用户信息赋值到session
                Session["Login"] = ManagePass.Encrypt(JsonConvert.SerializeObject(new LoginInfo {
                    UserName = loginUsername, BusinessID = userInfo.BussinessID, CompanyName = userInfo.CompanyName, UserManageID = userInfo.Id
                }), encryptKey);
                return(Json(new { result = true, msg = "登录成功" }));
            }
            else
            {
                return(Json(new { result = false, msg = "账号密码不正确,请重新输入" }));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 初始化数据库连接
        /// </summary>
        private static void InitDbConnectionStringConfig()
        {
            var encryptKey = AppSettingUtil.EncryptKey;
            var dbConnectionStringConfig = new DbConnectionStringConfig();
            var encryptConStr            = ConfigurationManager.ConnectionStrings["VideoDBConnectionString"].ToString();

            dbConnectionStringConfig.RolePermissionDbConnectionString = ManagePass.Decrypt(encryptConStr, encryptKey);

            DbConnectionStringConfig.InitDefault(dbConnectionStringConfig);
            Database.SetInitializer <VideoDbContext>(null);
            //是否使用with(nolock)
            DbInterception.Add(new NoLockInterceptor());
            NoLockInterceptor.IsEnableNoLock = true;
        }
        /// <summary>
        /// 身份验证,Action执行前判断
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var           isError   = false;
            StringBuilder logBuiler = new StringBuilder();
            var           loginUser = filterContext.HttpContext.Session[SESSIONKEY];

            try
            {
                #region 1.Session不为空,写入Cookie
                if (loginUser != null)
                {
                    if (Request.Cookies[COOKIEKEY] == null)
                    {
                        //-------Cookie写入----------
                        HttpCookie cookie = new HttpCookie(COOKIEKEY);          //定义cookie对象以及名为Info的项
                        DateTime   dt     = DateTime.Now;                       //定义时间对象
                        TimeSpan   ts     = new TimeSpan(0, 12, 30, 0);         //cookie有效作用时间,具体查msdn (0, 12, 0, 0)
                        cookie.Expires = dt.Add(ts);                            //添加作用时间
                        cookie.Values.Add("userfineral", loginUser.ToString()); //增加属性
                        Response.AppendCookie(cookie);                          //确定写入cookie中
                                                                                //--------------------------
                    }
                    else
                    {
                        string cookUserStr = Convert.ToString(Request.Cookies[COOKIEKEY].Values["userfineral"]);
                        if (!string.IsNullOrWhiteSpace(cookUserStr) && loginUser.ToString() != cookUserStr)
                        {
                            //-------Cookie写入----------
                            HttpCookie cookie = new HttpCookie(COOKIEKEY);          //定义cookie对象以及名为Info的项
                            DateTime   dt     = DateTime.Now;                       //定义时间对象
                            TimeSpan   ts     = new TimeSpan(0, 3, 0, 0);           //cookie有效作用时间,具体查msdn (0, 12, 0, 0)
                            cookie.Expires = dt.Add(ts);                            //添加作用时间
                            cookie.Values.Add("userfineral", loginUser.ToString()); //增加属性
                            Response.AppendCookie(cookie);                          //确定写入cookie中
                                                                                    //--------------------------
                        }
                    }
                }
                #endregion

                #region 2.Session为空,Cookie不为空时,通过Cookie来给Session重新赋值
                if (loginUser == null && Request.Cookies[COOKIEKEY] != null)
                {
                    string loginUserStr = Convert.ToString(Request.Cookies[COOKIEKEY].Values["userfineral"]);
                    if (!string.IsNullOrWhiteSpace(loginUserStr))
                    {
                        try
                        {
                            LoginInfo loginUserCookie = JsonConvert.DeserializeObject <LoginInfo>(ManagePass.Decrypt(loginUserStr, encryptKey));
                            logBuiler.Append(string.Format("{0}\r\n{1}\r\n{2}\\r\n", loginUserCookie.UserName, "使用过cookie恢复session登录", "用户session"));
                            filterContext.HttpContext.Session[SESSIONKEY] = loginUserStr;
                            filterContext.HttpContext.Session.Timeout     = 180;
                        }
                        catch (Exception ex)
                        {
                            isError = true;
                            logBuiler.Append(string.Format("{0}\r\n{1}\r\n{2}", "cookie读取 失败", ex.ToString(), "Exception"));
                        }
                    }
                }
                #endregion

                #region 3.Session为空时重新登录平台
                if (filterContext.HttpContext.Session[SESSIONKEY] == null)
                {
                    logBuiler.Append($"SESSION为空,重新登录后台,地址:/Login/Index\r\n");
                    //alert('由于您长时间未操作页面,请重新登录');
                    filterContext.HttpContext.Response.Write($"<script type='text/javascript'>top.location='/Login/Index'</script>");
                    filterContext.HttpContext.Response.End();
                }
                #endregion
            }
            catch (Exception ex)
            {
                isError = true;
                logBuiler.Append($"OnActionExecuting=>【{DateTime.Now}】获取登录信息异常:{ex.ToString() + ex.Message}\r\n");
            }
            finally
            {
                if (!string.IsNullOrWhiteSpace(logBuiler.ToString()))
                {
                    if (isError)
                    {
                        LogWriter.error(logBuiler.ToString(), LogWriter.GetFramesString());
                    }
                    else
                    {
                        LogWriter.info(logBuiler.ToString(), LogWriter.GetFramesString());
                    }
                }
            }
            base.OnActionExecuting(filterContext);
        }