Exemplo n.º 1
0
        public ApiResult Login()
        {
            string account  = Request.Form["Account"];
            string passWord = Request.Form["Password"];

            Blog.Domain.User user = new Blog.Domain.User();
            try
            {
                user = _userService.SelectUser(account, passWord);
                IList <Claim> claims = new List <Claim>()
                {
                    new Claim("account", user.Account),
                    new Claim("username", user.Username),
                    new Claim("sex", user.Sex.GetEnumText <Sex>()),
                    new Claim("birthDate", user.BirthDate.HasValue?user.BirthDate.Value.ToString("yyyy-MM-dd"):""),
                    new Claim("email", string.IsNullOrEmpty(user.Email)?"":user.Email),
                    new Claim("sign", string.IsNullOrEmpty(user.Sign)?"":user.Sign),
                    new Claim("phone", string.IsNullOrEmpty(user.Phone)?"":user.Phone),
                    new Claim("headPhoto", string.IsNullOrEmpty(user.HeadPhoto)?"":user.HeadPhoto)
                };
                string jwtToken = new JWT(_cacheClient).CreateToken(claims);
                return(ApiResult.Success(jwtToken));
            }
            catch (ValidationException e)
            {
                return(ApiResult.AuthError(e.Message));
            }
        }
Exemplo n.º 2
0
        public ApiResult IsLogin()
        {
            bool noLogin = Response.Headers.TryGetValue("auth", out StringValues value);

            if (noLogin)
            {
                return(ApiResult.AuthError());
            }
            return(ApiResult.Success());;
        }
Exemplo n.º 3
0
        public ApiResult Publish()
        {
            string content = Request.Form["content"];

            try
            {
                UserModel userModel = Auth.GetLoginUser(_httpContext);
                Whisper   whisper   = new Whisper(userModel.Account, content);
                _whisperService.Insert(whisper);
                return(ApiResult.Success());
            }
            catch (AuthException)
            {
                return(ApiResult.AuthError());
            }
        }
Exemplo n.º 4
0
 public ApiResult GetLoginUser()
 {
     try
     {
         bool noLogin = Response.Headers.TryGetValue("auth", out StringValues value);
         if (noLogin)
         {
             throw new AuthException();
         }
         string    json      = new JWT(_context).ResolveToken();
         UserModel userModel = JsonHelper.DeserializeObject <UserModel>(json);
         return(ApiResult.Success(userModel));
     }
     catch (AuthException)
     {
         return(ApiResult.AuthError());
     }
 }
Exemplo n.º 5
0
        public ApiResult AddComment()
        {
            string content     = Request.Form["content"];
            int    whisperId   = Convert.ToInt32(Request.Form["whisperId"]);
            string revicer     = Request.Form["revicer"];
            string replyId     = Request.Form["replyId"];
            int    commentType = Convert.ToInt32(Request.Form["commentType"]);

            try
            {
                CommentModel commentModel = new CommentModel();
                commentModel.Content        = content;
                commentModel.AdditionalData = replyId;
                commentModel.PostUser       = Auth.GetLoginUser(_httpContext).Account;
                commentModel.Revicer        = revicer;
                commentModel.CommentType    = commentType;
                _whisperService.Review(commentModel, whisperId);
                return(ApiResult.Success());
            }
            catch (AuthException)
            {
                return(ApiResult.AuthError());
            }
        }