コード例 #1
0
        //在角色管理里自动勾选的父级菜单不会保存到AppRoleMenu表,所以这里
        //要重新取出子级被授权的父级菜单
        private async Task <IList <AppMenu> > GetUserMenus(string userId)
        {
            var allMenus = await _menu.GetAllAsync();

            DataGateService ds          = Consts.Get <DataGateService>();
            var             userMenuIds = ((JArray)(await ds.QueryAsync("GetUserMenuIds", new { userId })))
                                          .Select(dr => (string)dr.First);

            userMenuIds = userMenuIds.Select(id => GetParentIds(allMenus, id)).UnionAll();
            return(allMenus.Where(m => userMenuIds.Contains(m.Id)).ToList());
        }
コード例 #2
0
        async Task <AppUser> CreateTempUser(string tempId)
        {
            DataGateService dataSvc = Consts.Get <DataGateService>();
            var             newUser = new AppUser
            {
                Id       = tempId,
                Account  = tempId,
                Name     = tempId,
                Password = CommOp.NewId().Substring(8, 16)
            };

            await dataSvc.SubmitAsync("SaveUser", new
            {
                Added = new object[] { newUser }
            });

            return(newUser);
        }
コード例 #3
0
        /// <summary>
        /// 注册新用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <ApiResult> RegisterAsync(AppUser user)
        {
            var result = await CheckNewUserAsync(user);

            DataGateService dataSvc    = Consts.Get <DataGateService>();
            var             existsUser = await _user.GetModelByIdAsync(user.Id);

            if (existsUser == null)
            {
                throw new Exception("非法的注册信息");
            }
            await dataSvc.SubmitAsync("SaveUser", new
            {
                Changed = new object[] { user }
            });

            result.Message = "注册成功";
            return(result);
        }
コード例 #4
0
        /// <summary>
        /// 根据token获取用户信息,将用户表所有字段(除密码等信息外)返回给客户端
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <object> GetUserAsync(string token)
        {
            var userSession = await Get(token);

            if (userSession == null)
            {
                return(MSG.SessionExpired);
            }
            DataGateService ds   = Consts.Get <DataGateService>();
            var             user = await ds.QueryAsync("GetUser", new { userSession.Id }) as Dictionary <string, object>;

            if (user == null)
            {
                return(MSG.UserNotExists);
            }

            user["menus"] = await GetUserMenus(userSession.Id);

            return(user);
        }
コード例 #5
0
        /// <summary>
        /// 登录,根据用户名,手机,邮箱来登录,当同一手机,邮箱不止一个用户使用时,将登录不成功
        /// </summary>
        /// <param name="request"></param>
        /// <param name="validate">验证密码</param>
        /// <returns></returns>
        public async Task <LoginResult> Login(LoginRequest request, bool validate = true)
        {
            LoginResult result      = new LoginResult();
            string      requestPass = null;

            //登录时回传的记住我的信息,从记住我的信息恢复用户的登录用户名密码
            if (request.Remember?.Length > 10)
            {
                RestoreFormRemember(request);
                requestPass = request.Password;
            }
            AppUser user = null;

            request.Account = request.Account.ToLower();
            if (user == null && CommOp.IsEmail(request.Account))
            {
                user = await _user.GetByEmailAsync(request.Account);
            }
            if (user == null && CommOp.IsPhoneNumber(request.Account))
            {
                user = await _user.GetByTelAsync(request.Account);
            }
            if (user == null)
            {
                user = await _user.GetAsync(request.Account);
            }
            if (user == null)
            {
                return(MSG.UserNotExists);
            }

            if (requestPass == null)
            {
                requestPass = Encryption.MD5(request.Password + user.PasswordSalt);
            }
            if (user.Password != requestPass && validate)
            {
                return(MSG.PasswordError);
            }
            UserSession session = new UserSession
            {
                Token      = CommOp.NewId(),
                Account    = user.Account,
                Id         = user.Id,
                LastOpTime = DateTime.Now
            };
            var cacheKey     = GetCacheKey(session.Token);
            var sessionBytes = Encoding.UTF8.GetBytes(session.ToJson(false));

            await this.DistributedCache.SetAsync(cacheKey, sessionBytes, new DistributedCacheEntryOptions()
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(this.Expires)
            });

            this.MemoryCache.Set(cacheKey, session);

            DataGateService ds = Consts.Get <DataGateService>();

            //写最后登录时间,并进一步判断用户是否存在
            await ds.UpdateAsync("UpdateLastLoginTime", new
            {
                id            = user.Id,
                LastLoginDate = session.LastOpTime
            });

            //要求“记住我”时,将登录信息加密回传,根据服务端的加密
            if (request.Remember == "1")
            {
                request.Remember = Encryption.Encrypt(String.Join("|", user.Account, user.Password));
            }
            return(new LoginResult
            {
                ExpireIn = Expires,
                Token = session.Token,
                Remember = request.Remember
            });
        }
コード例 #6
0
        /// <summary>
        /// 登录,根据用户名,手机,邮箱来登录,当同一手机,邮箱不止一个用户使用时,将不成登录成功
        /// </summary>
        /// <param name="request"></param>
        /// <param name="validate">验证密码</param>
        /// <returns></returns>
        public async Task <LoginResult> Login(LoginRequest request, bool validate = true)
        {
            LoginResult result      = new LoginResult();
            string      requestPass = null;

            //登录时回传的记住我的信息,从记住我的信息恢复用户的登录用户名密码
            if (request.Remember?.Length > 10)
            {
                RestoreFormRemember(request);
                requestPass = request.Password;
            }
            AppUser user = null;

            if (user == null && CommOp.IsEmail(request.Account))
            {
                user = await _user.GetByEmailAsync(request.Account);
            }
            if (user == null && CommOp.IsPhoneNumber(request.Account))
            {
                user = await _user.GetByTelAsync(request.Account);
            }
            if (user == null)
            {
                user = await _user.GetAsync(request.Account);
            }
            if (user == null)
            {
                return(MSG.UserNotExists);
            }

            if (requestPass == null)
            {
                requestPass = Encryption.MD5(request.Password + user.PasswordSalt);
            }
            if (user.Password != requestPass && validate)
            {
                return(MSG.PasswordError);
            }
            UserSession session = new UserSession
            {
                Token      = CommOp.NewId(),
                Account    = user.Account,
                Id         = user.Id,
                LastOpTime = DateTime.Now
            };

            _sessionDict.TryAdd(session.Token, session);

            DataGateService ds = Consts.Get <DataGateService>();
            await ds.UpdateAsync("UpdateLastLoginTime", new
            {
                id            = user.Id,
                LastLoginDate = session.LastOpTime
            });

            //要求“记住我”时,将登录信息加密回传,根据服务端的加密
            if (request.Remember == "1")
            {
                request.Remember = Encryption.Encrypt(String.Join("|", user.Account, user.Password));
            }
            return(new LoginResult
            {
                ExpireIn = Expires,
                Token = session.Token,
                Remember = request.Remember
            });
        }