Exemple #1
0
        public async Task <ResillienceResult <string> > GetAccessTokenAsync(string code)
        {
            var result = new ResillienceResult <string>();

            if (string.IsNullOrEmpty(code))
            {
                result.IsFailed("code为空");
                return(result);
            }
            var request = new AccessTokenRequest();
            var content = new StringContent($"code={code}&client_id={request.Client_ID}&redirect_uri={request.Redirect_Uri}&client_secret={request.Client_Secret}");

            content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
            using var client            = _httpClient.CreateClient();
            var httpResponse = await client.PostAsync(GitHubConfig.API_AccessToken, content);

            var response = await httpResponse.Content.ReadAsStringAsync();

            if (response.StartsWith("access_token"))
            {
                result.IsSuccess(response.Split("=")[1].Split("&").First());
            }
            else
            {
                result.IsFailed("code不正确");
            }
            return(result);
        }
Exemple #2
0
        public async Task <ResillienceResult <string> > GenerateTokenAsync(string access_token)
        {
            var result = new ResillienceResult <string>();

            if (string.IsNullOrEmpty(access_token))
            {
                result.IsFailed("access_token为空");
                return(result);
            }
            var url = $"{GitHubConfig.API_User}?access_token={access_token}";

            using var client = _httpClient.CreateClient();
            client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.14 Safari/537.36 Edg/83.0.478.13");
            var httpResponse = await client.GetAsync(url);

            if (httpResponse.StatusCode != HttpStatusCode.OK)
            {
                result.IsFailed("access_token不正确");
                return(result);
            }
            var content = await httpResponse.Content.ReadAsStringAsync();

            var user = content.DeJson <UserResponse>();

            if (user.IsNull())
            {
                result.IsFailed("未获取到用户数据");
                return(result);
            }
            //if (user.Id != GitHubConfig.UserId)
            //{
            //    result.IsFailed("当前账号未授权");
            //    return result;
            //}
            var claims = new[] {
                new Claim(ClaimTypes.Name, user.Login),
                new Claim(ClaimTypes.Email, user.Login),
                new Claim(JwtRegisteredClaimNames.Exp, $"{new DateTimeOffset(DateTime.Now.AddMinutes(_settings.JWT.Expires)).ToUnixTimeSeconds()}"),
                new Claim(JwtRegisteredClaimNames.Nbf, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}")
            };
            var key           = new SymmetricSecurityKey(_settings.JWT.SecurityKey.SerializeUtf8());
            var creds         = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var securityToken = new JwtSecurityToken(
                issuer: _settings.JWT.Domain,
                audience: _settings.JWT.Domain,
                claims: claims,
                expires: DateTime.Now.AddMinutes(_settings.JWT.Expires),
                signingCredentials: creds);
            var token = new JwtSecurityTokenHandler().WriteToken(securityToken);

            result.IsSuccess(token);
            return(await Task.FromResult(result));
        }
Exemple #3
0
        public ResillienceResult <IQueryable <SmsReseponceDTO> > SearchMessage(SearchMessageRequestDTO req)
        {
            var result     = new ResillienceResult <IQueryable <SmsReseponceDTO> >();
            var expression = LinqExtensions.True <SmsMessage>();

            if (req.Status.HasValue)
            {
                expression = expression.And(a => a.Status == req.Status.Value);
            }

            if (req.Type.HasValue)
            {
                expression = expression.And(a => a.Type == req.Type.Value);
            }

            if (req.BeganCreateTime.HasValue)
            {
                expression = expression.And(a => a.CreateTime >= req.BeganCreateTime.Value);
            }

            if (req.EndCreateTime.HasValue)
            {
                expression = expression.And(a => a.CreateTime <= req.EndCreateTime.Value);
            }

            if (req.BeganTimeSendTime.HasValue)
            {
                expression = expression.And(a => a.TimeSendTime >= req.BeganTimeSendTime.Value);
            }

            if (req.EndTimeSendTime.HasValue)
            {
                expression = expression.And(a => a.TimeSendTime <= req.EndTimeSendTime.Value);
            }

            //if (!string.IsNullOrEmpty(req.Mobile))
            //    expression = expression.And(a => a.Mobiles.Contains(req.Mobile));

            if (!string.IsNullOrEmpty(req.Content))
            {
                expression = expression.And(a => a.Content.Contains(req.Content));
            }

            var res = _smsMessageRepository.GetAll().Where(expression).ProjectTo <SmsReseponceDTO>(_mapper.ConfigurationProvider);

            if (res == null)
            {
                result.IsFailed("数据不存在");
                return(result);
            }
            result.IsSuccess(res);
            return(result);
        }
Exemple #4
0
        /// <summary>
        /// 列表数据不分页
        /// </summary>
        /// <returns></returns>
        public ResillienceResult <IQueryable <Post> > GetAll()
        {
            var result = new ResillienceResult <IQueryable <Post> >();
            var post   = _postRepository.GetAll().AsNoTracking();

            if (post == null)
            {
                result.IsFailed("数据不存在");
                return(result);
            }
            result.IsSuccess(post);
            return(result);
        }
Exemple #5
0
        public ResillienceResult <SmsReseponceDTO> GetById(long id)
        {
            var result = new ResillienceResult <SmsReseponceDTO>();
            var res    = _mapper.Map <SmsReseponceDTO>(_smsMessageRepository.GetByIdAsync(id));

            if (res == null)
            {
                result.IsFailed("数据不存在");
                return(result);
            }
            result.IsSuccess(res);
            return(result);
        }
Exemple #6
0
        public async Task <ResillienceResult <string> > GetLoginAddressAsync()
        {
            var result  = new ResillienceResult <string>();
            var request = new AuthorizeRequest();
            var address = string.Concat(new string[]
            {
                GitHubConfig.API_Authorize,
                "?client_id=", request.Client_ID,
                "&scope=", request.Scope,
                "&state=", request.State,
                "&redirect_uri=", request.Redirect_Uri
            });

            result.IsSuccess(address);
            return(await Task.FromResult(result));
        }