Example #1
0
        /// <summary>检查登录,默认检查密码MD5散列,可继承修改</summary>
        /// <param name="user">用户名</param>
        /// <param name="pass">密码</param>
        /// <returns>返回要发给客户端的对象</returns>
        protected override Object CheckLogin(String user, String pass)
        {
            if (user.IsNullOrEmpty())
            {
                throw Error(3, "用户名不能为空");
            }

            var dic = ControllerContext.Current?.Parameters?.ToNullable();

            if (dic != null)
            {
                Agent   = dic["Agent"] + "";
                OS      = dic["OS"] + "";
                Type    = dic["Type"] + "";
                Version = dic["Version"] + "";
            }
            // 登录
            Name = user;

            CheckOnline(user);

            var msg = "登录 {0}/{1}".F(user, pass);

            WriteLog(msg);

            var ns   = Session as NetSession;
            var flag = true;
            var act  = "Login";

            try
            {
                Object rs = null;

                // 查找并登录,找不到用户是返回空,登录失败则抛出异常
                var u = CheckUser(user, pass);

                // 注册
                if (u == null)
                {
                    act = "Register";
                    u   = Register(user, pass);
                    if (u == null)
                    {
                        throw Error(3, user + " 禁止注册");
                    }

                    if (u.ID == 0)
                    {
                        u.SaveRegister(ns);
                    }

                    rs = new { user = u.Name, pass = u.Password };
                }
                // 登录
                else
                {
                    if (!u.Enable)
                    {
                        throw Error(4, user + " 已被禁用");
                    }

                    if (AuthKey.IsNullOrEmpty())
                    {
                        rs = new { Name = u + "" }
                    }
                    ;
                    else
                    {
                        rs = new { Name = u + "", Key = AuthKey }
                    };
                }

                //u.SaveLogin(ns);
                SaveLogin(u);

                // 当前设备
                Current           = u;
                Session.UserState = u;

                var olt = Online;
                if (olt.UserID > 0 && olt.UserID != u.ID)
                {
                    SaveHistory("Logout", true, "=> " + u);
                }
                olt.UserID = u.ID;
                olt.SaveAsync();

                // 销毁时
                ns.OnDisposed += (s, e) =>
                {
                    Online.Delete();

                    SaveHistory("Logout", true, null);
                };

                return(rs);
            }
            catch (Exception ex)
            {
                msg += " " + ex?.GetTrue()?.Message;
                flag = false;
                throw;
            }
            finally
            {
                SaveHistory(act, flag, msg);
            }
        }