コード例 #1
0
        public async Task UserInfo([QueryField] string token)
        {
            // 用 token 获取用户信息
            UserConfig uConfig  = IoC.Get <UserConfig>();
            JwtToken   jwtToken = new JwtToken(uConfig.TokenSecret, token);

            if (jwtToken.TokenValidState != TokenValidState.Valid)
            {
                await ResponseErrorAsync("token无效");

                return;
            }


            // 返回用户信息
            var user = LiteDb.Query <User>().Where(u => u.userId == jwtToken.UserId).FirstOrDefault();

            if (user == null)
            {
                await ResponseErrorAsync("未找到用户!");

                return;
            }

            if (string.IsNullOrEmpty(user.avatar))
            {
                user.avatar = uConfig.DefaultAvatar;
            }

            await ResponseSuccessAsync(user);
        }
コード例 #2
0
        public async Task UserLogin()
        {
            // 读取jsonData
            var body = JObject.Parse(await HttpContext.GetRequestBodyAsStringAsync());

            string userId   = body.SelectToken(Fields.userName).ValueOrDefault(string.Empty);
            string password = body.SelectToken(Fields.password).ValueOrDefault(string.Empty); // 由于是客户端,不加密

            // 判断数据正确性
            if (string.IsNullOrEmpty(userId))
            {
                await ResponseErrorAsync("用户名为空");
            }

            if (string.IsNullOrEmpty(password))
            {
                await ResponseErrorAsync("密码为空");

                return;
            }

            // 获取数据库
            var user = LiteDb.Query <User>().Where(u => u.userId == userId).FirstOrDefault();

            if (user == null)
            {
                // 新建用户
                LiteDb.Insert(new User()
                {
                    userId     = userId,
                    password   = password,
                    createDate = DateTime.Now
                });

                // 新建用户后,同时给用户建立默认配置
                LiteDb.Insert(Setting.DefaultSetting(userId));
            }
            else
            {
                // 判断密码正确性
                if (user.password != password)
                {
                    await ResponseErrorAsync("密码错误");

                    return;
                }
            }

            UserConfig uConfig  = IoC.Get <UserConfig>();
            JwtToken   jwtToken = new JwtToken(uConfig.TokenSecret, userId, JwtToken.DefaultExp());

            await ResponseSuccessAsync(new JObject(new JProperty(Fields.token, jwtToken.Token)));
        }