コード例 #1
0
        private void attachUserToContext(HttpContext context, IRespository.IRepository <User> _userService, 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,
                    // set clockskew to zero so tokens expire exactly at token expiration time (instead of 5 minutes later)
                    ClockSkew = TimeSpan.Zero
                }, out SecurityToken validatedToken);

                var jwtToken = (JwtSecurityToken)validatedToken;
                var userId   = Guid.Parse(jwtToken.Claims.First(x => x.Type == "id").Value);

                // attach user to context on successful jwt validation
                context.Items["User"] = _userService.Get(userId);
            }
            catch
            {
                // do nothing if jwt validation fails
                // user is not attached to context so request won't have access to secure routes
            }
        }
コード例 #2
0
        public async Task Invoke(HttpContext context, IRespository.IRepository <User> userService)
        {
            var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();

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

            await _next(context);
        }
コード例 #3
0
 public AccountController(IRespository.IRepository <User> userRepository, IAccount account)
 {
     _userRepository = userRepository;
     _account        = account;
 }
コード例 #4
0
 public RoleController(IRespository.IRepository <Role> roleRepository)
 {
     _roleRepository = roleRepository;
 }
コード例 #5
0
 public Account(IRespository.IRepository <User> context, IJwtAuth auth)
 {
     _context = context;
     _auth    = auth;
 }