Ejemplo n.º 1
0
 public Result Add([FromForm] string name, [FromForm] string password)
 {
     if (!User.Super)
     {
         return(Fail("添加用户失败,你没有权限操作"));
     }
     try
     {
         if (_repository.Exists(n => n.Name == name))
         {
             return(Fail("用户名称已存在!"));
         }
         UserInfo user = new UserInfo {
             Name = name, Password = UserUtility.EncryptPassword(password)
         };
         if (_repository.Insert(user) <= 0)
         {
             return(Fail("添加用户失败!"));
         }
         user.Password = password;
         return(Success(user, "添加成功!"));
     }
     catch (Exception ex)
     {
         _logger.LogError(ex.Message + ex.StackTrace);
         return(Fail("系统错误"));
     }
 }
Ejemplo n.º 2
0
        public Result Login([FromForm] int Userid, [FromForm] string Password)
        {
            try
            {
                var user = _repository.FindFirst(n => n.Id == Userid);
                if (user == null)
                {
                    return(Fail("登录失败,用户不存在!"));
                }
                if (user.Password != UserUtility.EncryptPassword(Password))
                {
                    return(Fail("登录失败,登录密码错误!"));
                }

                string        token   = UserUtility.GetTokenById(user.Id);
                CookieOptions options = new CookieOptions();
                options.Expires = DateTime.Now.AddDays(_config.TokenDays);
                Response.Cookies.Append("token", token, options);

                _cache.SetUser(user);

                return(Success(new { Id = user.Id, Name = user.Name, user.Super }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message + ex.StackTrace);
                return(Fail("系统错误"));
            }
        }