Exemplo n.º 1
0
        public async Task Invoke(HttpContext context, IGetByIdUserFlow getByIdUserFlow)
        {
            var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();

            if (token != null)
            {
                attachUserToContext(context, getByIdUserFlow, token);
            }

            await _next(context);
        }
Exemplo n.º 2
0
        private async void attachUserToContext(HttpContext context, IGetByIdUserFlow getByIdUserFlow, string token)
        {
            try
            {
                var tokenHandler = new JwtSecurityTokenHandler();
                var key          = Encoding.ASCII.GetBytes(_appSettings.Secret);
                tokenHandler.ValidateToken(token, new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false,
                    ClockSkew = TimeSpan.Zero
                }, out SecurityToken validatedToken);

                var jwtToken = (JwtSecurityToken)validatedToken;
                var userId   = int.Parse(jwtToken.Claims.First(x => x.Type == "id").Value);
                context.Items["User"] = await getByIdUserFlow.Execute(userId);
            }
            catch
            {
            }
        }