Beispiel #1
0
    public string IniciateUserSession(int userId)
    {
        var    session = UserSession.Where(w => w.UserId == userId).FirstOrDefault();
        string sessionToken;

        if (session == null)
        {
            sessionToken = GenerateSessionToken();

            UserSession newUserSession = new UserSession
            {
                UserId       = userId,
                SessionToken = sessionToken,
                ExpireDate   = DateTime.Now.AddDays(1)
            };

            UserSession.Add(newUserSession);

            SaveChanges();
        }
        else
        {
            sessionToken = session.SessionToken;
        }

        return(sessionToken);
    }
        /// <summary>
        ///
        /// </summary>
        /// <param name="loginData"></param>
        /// <returns></returns>
        public object Login(getLogin loginData)
        {
            t_bas_user tBasUser = null;

            if (!ValidateUser(loginData.strUser, loginData.strPwd, out tBasUser))
            {
                return(new { bRes = false, message = "账号或密码不正确!" });
            }
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(0, tBasUser.Code, DateTime.Now,
                                                                             DateTime.Now.AddHours(1), true, string.Format("{0}&{1}", tBasUser.Code, tBasUser.Password),
                                                                             FormsAuthentication.FormsCookiePath);
            webDmsEntities db = new webDmsEntities();

            //var homeOjb = new object[] { new { path = "/", iconCls = "fa fa-home", leaf = true, children = new object[] { new { path = "/index", MenuPath = "index", meta = new { name = "主页", button = new string[] { }.ToList() } } } } };

            var list = db.view_menu.Where <view_menu>(p => p.UserID.ToString() == tBasUser.UserID.ToString() && p.ParentCode == "&").Select(s => new
            {
                path     = "/",
                name     = s.Name,
                url      = "",
                meta     = new { name = s.Name, button = new string[0] {
                                 }.ToList() },
                Xh       = s.Sequence,
                MenuID   = s.Code,
                iconCls  = s.ICON,
                children = db.view_menu.Where <view_menu>(p1 => p1.UserID.ToString() == tBasUser.UserID.ToString() && p1.ParentCode == s.Code).Select(s1 => new
                {
                    path     = "/" + s1.URL,
                    url      = s1.URL,
                    name     = s1.Name,
                    meta     = new { name = s1.Name, button = new string[] { "save", "cancle", "new", "audit" }.ToList(), isButton = false },
                    MenuPath = s1.URL.Replace("_", "/"),
                    Xh       = s1.Sequence,
                    MenuID   = s1.Code
                }).OrderBy(o => o.Xh).ThenBy(o => o.MenuID).ToList()
            }).OrderBy(o => o.Xh).ThenBy(o => o.MenuID).ToList();

            var tempList = list.ToList();

            //返回登录结果、用户信息、用户验证票据信息
            var    tSysCompany = db.t_sys_company.Where(w => w.CorpID == tBasUser.CorpID);
            string trademark   = tSysCompany.Join(db.t_bas_company, a => a.CorpID, b => b.CorpID, (a, b) => b.TradeMark).FirstOrDefault();
            string TradeMark   = "/" + VirtualPath + "/" + UploadImgPath + "/" + trademark;      //获取当前项目所在目录
            string avatar      = "/" + VirtualPath + "/" + UploadImgPath + "/" + tBasUser.Photo; //获取当前项目所在目录
            var    oUser       = new UserInfo {
                bRes = true, user = new { name = tBasUser.Name, avatar = avatar, TradeMark = TradeMark }, Ticket = FormsAuthentication.Encrypt(ticket), menu = tempList
            };

            //将经销商权限保存在session中
            UserSession.Add("CompanyRightsID", tSysCompany.FirstOrDefault().RightsID);
            //HttpContext.Current.Session["CompanyRightsID"] = tSysCompany.FirstOrDefault().RightsID;
            return(oUser);
        }
        //校验用户名密码(正式环境中应该是数据库校验)
        private bool ValidateUser(string strUser, string strPwd, out t_bas_user userinfo)
        {
            webDmsEntities db       = new webDmsEntities();
            string         password = Sha1Encrypt(strPwd);

            var list = db.t_bas_user.FirstOrDefault(p => p.Code == strUser && p.Password == password);

            userinfo = list;

            if (list != null)
            {
                //HttpContext.Current.Session["UserInfo"] = list;
                UserSession.Add("UserInfo", list);
                return(true);
            }
            else
            {
                return(false);
            }
        }