示例#1
0
        public void Login()
        {
            try
            {
                string UserCode   = Request["username"];
                string Password   = Request["password"];
                string VerifyCode = Request["verifycode"];

                if (string.IsNullOrEmpty(UserCode))
                {
                    throw new Exception("用户名不能为空");
                }

                if (VerifyCode.ToLower() != Session["LoginVerifyCode"].ToString().ToLower())
                {
                    throw new PException("验证码不正确");
                }

                using (ProxyBE be = new ProxyBE())
                {
                    #region 登录
                    User user = be.Client.GetUserByUserCode(null, UserCode);

                    if (user == null)
                    {
                        throw new PException("errorUserName");
                    }
                    if (user.IsLocked)
                    {
                        throw new PException("errorLockedUserName");
                    }
                    if (user.IsDisabled)
                    {
                        throw new PException("errorDisabledUserName");
                    }
                    if (user.LoginErrorCount >= 5)
                    {
                        throw new PException("errorLoginOutTimes");
                    }

                    SaveUserArgs args = new SaveUserArgs();
                    //记录当前登录时间
                    if (CEncrypt.EncryptString(Password) != user.Password)
                    {
                        user.LoginErrorCount += 1;
                        PException ex = null;
                        if (user.LoginErrorCount >= 5)
                        {
                            user.IsLocked = true;
                            ex            = new PException("errorLoginOutTimes");
                        }
                        else
                        {
                            ex = new PException("errorPassword{0}", 5 - user.LoginErrorCount);
                        }
                        args.User = user;
                        be.Client.SaveUser(base.SenderUser, args);
                        throw ex;
                    }


                    args.User            = user;
                    user.LoginErrorCount = 0;
                    user.LastLoginTime   = DateTime.Now;
                    be.Client.SaveUser(base.SenderUser, args);

                    SessionUser su = new SessionUser();
                    su.UserCode = user.UserCode;
                    //用户类型
                    su.UserType = (int)UserType.U;
                    //su.LoginUrl = UserLoginUrl;
                    su.UserID        = user.UserID;
                    su.UserName      = user.UserName;
                    su.IsSystemUser  = user.IsSystem;
                    su.LastLoginTime = user.LastLoginTime;
                    su.CompanyID     = user.CompanyID;
                    OnlineUser.Lock(su);
                    this.CurrentUser = su;
                    #endregion

                    #region 加载权限项
                    List <Role> userRoles = be.Client.GetRolesByUserID(null, user.UserID);
                    su.Roles = userRoles;

                    List <PrivilegeItem> privilegeItems = be.Client.GetPrivilegeItemByUserID(null, user.UserID);
                    var privilegeItemS = from pi in privilegeItems select pi.PrivilegeItemID;
                    su.PrivilegeItemIDs = privilegeItemS.ToList <Guid>();

                    List <Privilege> privileges = be.Client.GetPrivilegesByUserID(null, user.UserID);
                    var privilegeS = from pl in privileges select pl.PrivilegeID;
                    su.PrivilegeIDs = privilegeS.ToList <Guid>();

                    Dictionary <string, List <string> > PrivilegeCodesDir = new Dictionary <string, List <string> >();
                    foreach (var item in privileges)
                    {
                        if (PrivilegeCodesDir.ContainsKey(item.PrivilegeCode.ToLower()))
                        {
                            continue;
                        }
                        List <string> itemCodes = new List <string>();
                        var           plItems   = privilegeItems.Where(pl => pl.PrivilegeID.ToString() == item.PrivilegeID.ToString() && pl.IsDisabled == false).ToList();
                        if (plItems != null)
                        {
                            foreach (var itemPrivilegeItems in plItems)
                            {
                                itemCodes.Add(itemPrivilegeItems.PrivilegeItemCode.ToLower());
                            }
                        }
                        PrivilegeCodesDir.Add(item.PrivilegeCode.ToLower(), itemCodes);
                    }
                    su.PrivilegeCodes = PrivilegeCodesDir;
                    #endregion

                    StringBuilder sb = new StringBuilder();
                    sb.Append('{');
                    sb.Append(string.Format("\"isOk\":\"{0}\",\"message\":\"{1}\",\"url\":\"{2}\"", 1, "success", "/Index.aspx"));
                    sb.Append('}');
                    Response.Write(sb.ToString());
                    //WriteSuccess();
                }
            }
            catch (Exception ex)
            {
                PLogger.LogError(ex);
                WriteError(ex.Message);
            }
        }
        public void Login()
        {
            try
            {
                string UserCode   = Request["username"];
                string Password   = Request["password"];
                string VerifyCode = Request["verifycode"];

                if (string.IsNullOrEmpty(UserCode))
                {
                    throw new Exception("用户名不能为空");
                }
                if (VerifyCode.ToLower() != Session["LoginVerifyCode"].ToString().ToLower())
                {
                    throw new PException("验证码错误");
                }

                using (ProxyBE p = new ProxyBE())
                {
                    #region 登录
                    PartnerUser partner = p.Client.GetPartnerUserByUserCode(null, UserCode);
                    if (partner == null)
                    {
                        throw new PException("用户不存在");
                    }
                    if (partner.IsLocked)
                    {
                        throw new PException("帐号被锁");
                    }
                    if (partner.IsDisabled)
                    {
                        throw new PException("帐号禁用");
                    }
                    if (partner.MemberClass == -1)
                    {
                        throw new PException("帐号待审核,请联系客服专员");
                    }
                    if (partner.LoginErrorCount >= 5)
                    {
                        throw new PException("密码错误次数过多,帐号已被锁定,请联系管理员");
                    }
                    if (DateTime.Now > partner.EndDate)
                    {
                        throw new PException("账户已过期,请联系客服续费");
                    }
                    SavePartnerUserArgs args = new SavePartnerUserArgs();
                    if (CEncrypt.EncryptString(Password) != partner.Password)
                    {
                        partner.LoginErrorCount += 1;
                        PException ex = null;
                        if (partner.LoginErrorCount >= 5)
                        {
                            //登录错误次数过多 帐号锁定
                            partner.IsLocked = true;
                            ex = new PException("密码错误次数过多,帐号已被锁定,请联系管理员");
                        }
                        else
                        {
                            //剩余登录次数
                            ex = new PException("密码错误,您还有{0}机会", 5 - partner.LoginErrorCount);
                        }
                        args.PartnerUser = partner;
                        p.Client.SavePartnerUser(base.SenderUser, args);
                        throw ex;
                    }

                    args.PartnerUser        = partner;
                    partner.LoginErrorCount = 0;
                    partner.LastLoginTime   = DateTime.Now;
                    p.Client.SavePartnerUser(base.SenderUser, args);
                    #endregion

                    #region Session

                    SessionUser su = new SessionUser();
                    su.UserCode  = partner.UserCode;
                    su.UserID    = partner.UserID;
                    su.PartnerID = partner.PartnerID;
                    su.Position  = partner.Position;
                    //su.LoginUrl = PartnerLoginUrl;
                    su.UserName      = partner.UserName;
                    su.UserType      = (int)UserType.D;
                    su.IsSystemUser  = partner.IsSystem;
                    su.LastLoginTime = partner.LastLoginTime;
                    su.IsFinishInfo  = partner.IsFinishInfo;
                    OnlineUser.Lock(su);
                    this.CurrentUser = su;

                    #endregion

                    #region 加载权限
                    //privilegeItem
                    List <PrivilegeItem> privilegeItems = p.Client.GetPrivilegeItemByPartnerUserID(null, partner.UserID);
                    var privilegeItemS = from pi in privilegeItems select pi.PrivilegeItemID;
                    su.PrivilegeItemIDs = privilegeItemS.ToList <Guid>();

                    //privileges
                    List <Privilege> privileges = p.Client.GetPrivilegesByPartnerUserID(null, partner.UserID);
                    var privilegeS = from pl in privileges select pl.PrivilegeID;
                    su.PrivilegeIDs = privilegeS.ToList <Guid>();

                    Dictionary <string, List <string> > PrivilegeCodesDir = new Dictionary <string, List <string> >();
                    foreach (var item in privileges)
                    {
                        if (PrivilegeCodesDir.ContainsKey(item.PrivilegeCode.ToLower()))
                        {
                            continue;
                        }
                        List <string> itemCodes = new List <string>();
                        var           plItems   = privilegeItems.Where(pl => pl.PrivilegeID.ToString() == item.PrivilegeID.ToString() && pl.IsDisabled == false).ToList();
                        if (plItems != null)
                        {
                            foreach (var itemPrivilegeItems in plItems)
                            {
                                itemCodes.Add(itemPrivilegeItems.PrivilegeItemCode.ToLower());
                            }
                        }
                        PrivilegeCodesDir.Add(item.PrivilegeCode.ToLower(), itemCodes);
                    }
                    su.PrivilegeCodes = PrivilegeCodesDir;
                    #endregion

                    string url = "/Index.aspx";
                    //if (MustChangePasswordAtFirstLogin)
                    //{
                    //    if (partner.Password == CEncrypt.EncryptString(UserDefaultPassword))
                    //    {
                    //        url = "/partnerModifypwd.aspx";
                    //    }
                    //}
                    StringBuilder sb = new StringBuilder();
                    sb.Append('{');
                    sb.Append(string.Format("\"isOk\":\"{0}\",\"message\":\"{1}\",\"url\":\"{2}\"", 1, "success", url));
                    sb.Append('}');
                    Response.Write(sb.ToString());
                }
            }
            catch (Exception ex)
            {
                PLogger.LogError(ex);
                WriteError(ex.Message);
            }
        }