public async Task InvokeAsync(HttpContext context)
        {
            var page = context.Request.Path.Value;

            if (page == "/authcode")
            {
                //根据code获取token
                var request     = context.Request;
                var host        = $"{request.Scheme}://{request.Host}";
                var code        = request.Query["code"];
                var state       = request.Query["state"];
                var returnUrl   = request.Query["return_url"];
                var redirectUri = HttpUtility.UrlEncode($"{host}/authcode?return_url={returnUrl}");
                var result      = await _authorizeTokenClient.GetToken(code, state, redirectUri);

                if (!string.IsNullOrEmpty(result.result.AccessToken))
                {
                    context.Response.Cookies.Append("jwt", result.result.AccessToken, new CookieOptions {
                        Expires = DateTimeOffset.Now.AddHours(2)
                    });
                    context.Response.Redirect(returnUrl);
                }
                return;
            }
            if (context.Request.Cookies.TryGetValue("jwt", out string token))
            {
                context.Request.Headers.Add("Authorization", $"Bearer {token}");
            }
            await _next(context);
        }
Esempio n. 2
0
        private async Task <(string, JwtToken)> GetCacheToken()
        {
            var key = $"client_credentials:{_appSettings.ClientId}";

            var tokenResult = await _redisCache.GetAsync <JwtToken>(key);

            if (!string.IsNullOrWhiteSpace(tokenResult?.AccessToken))
            {
                return("", tokenResult);
            }

            var(tokenerror, result) = await _authorizeTokenClient.GetToken();

            if (!string.IsNullOrEmpty(tokenerror))
            {
                return(tokenerror, null);
            }

            await _redisCache.AddAsync(key, result, TimeSpan.FromSeconds(int.Parse(result.ExpiresIn) - 300));

            return("", result);
        }
Esempio n. 3
0
        public async Task <IActionResult> Post([FromBody] AuthorizationCodeModel data)
        {
            var(error, result) = await _authorizeClient.GetToken(data.Code, data.State, data.RedirectUri);

            return(!string.IsNullOrEmpty(error) ? ObjectResponse.Ok(-1, error) : ObjectResponse.Ok(result));
        }