public async Task <UsersGetResponse> GetUserInfoAsync(string accessToken)
        {
            using (HttpClient client = new HttpClient())
            {
                UsersGetResponse response = null;

                try
                {
                    var result = await client.GetAsync(new UsersGetRequest()
                    {
                        AccessToken = accessToken,
                        Version     = API_VERSION
                    }
                                                       .ToString());

                    response = await result.Content.ReadAsAsync <UsersGetResponse>();
                }
                catch (HttpRequestException e)
                {
                    _logger.LogError(e.Message);
                }

                return(response);
            }
        }
Beispiel #2
0
        public string GenerateJwtToken(UsersGetResponse userInfo)
        {
            _logger.LogInformation(JsonConvert.SerializeObject(
                                       new
            {
                info = $"Generate JWT Token for user {userInfo.response[0].first_name} ({userInfo.response[0].id})"
            }
                                       ));

            var claims = new List <Claim>()
            {
                new Claim(ClaimTypes.NameIdentifier, userInfo.response[0].id.ToString()),
                new Claim(ClaimsIdentity.DefaultNameClaimType, userInfo.response[0].first_name),
                new Claim(ClaimsIdentity.DefaultRoleClaimType, "User")
            };

            var token = new JwtSecurityToken(
                issuer: _configurationManager.GetValue(ConfigurationKey.ISSUER),
                audience: _configurationManager.GetValue(ConfigurationKey.AUDIENCE),
                claims: claims,
                expires: DateTime.UtcNow.Add(TimeSpan.FromDays(1)),
                signingCredentials: new SigningCredentials(_configurationManager.GetSymmetricSecurityKey(ConfigurationKey.SIGNING_KEY), SecurityAlgorithms.HmacSha256)
                );

            return(new JwtSecurityTokenHandler().WriteToken(token));
        }
        public IActionResult GetAccessToken(string code, string state)
        {
            if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(state))
            {
                OAuthManager oauthManager = new OAuthManager(_logger);

                AccessTokenResponse response = oauthManager.GetAccessTokenAsync(
                    new AccessTokenRequest()
                {
                    ClientId     = _configuration.GetValue(ConfigurationKey.CLIENT_ID),
                    ClientSecret = _configuration.GetValue(ConfigurationKey.CLIENT_SECRET),
                    RedirectUri  = state,
                    Code         = code
                })
                                               .Result;

                if (response != null && response.access_token != null)
                {
                    UsersGetResponse userInfo = oauthManager.GetUserInfoAsync(response.access_token).Result;

                    if (userInfo != null && userInfo.response != null)
                    {
                        _logger.LogWarning(JsonConvert.SerializeObject(
                                               new
                        {
                            info = userInfo
                        }
                                               ));

                        HttpContext.Response.Cookies.Append("access_token", _jwtManager.GenerateJwtToken(userInfo));
                        return(Redirect("/"));
                    }

                    _logger.LogError(JsonConvert.SerializeObject(
                                         new
                    {
                        error = "Invalid user info"
                    }
                                         ));
                }

                _logger.LogError(JsonConvert.SerializeObject(
                                     new
                {
                    error = "Can't get access token"
                }
                                     ));
            }

            return(Unauthorized());
        }