Example #1
0
        public async Task Invoke(HttpContext context, IJwtAuthorizeHandler authorizeHandler)
        {
            try
            {
                if (!context.Request.Path.Value.EndsWith("login"))
                {
                    var sessionHeader = context.Request.Headers["authorization"].ToString();

                    if (string.IsNullOrEmpty(sessionHeader))
                    {
                        context.Response.StatusCode = 401;
                        throw new AuthenticationException();
                    }
                    if (sessionHeader.StartsWith(JwtBearerDefaults.AuthenticationScheme))
                    {
                        var token = sessionHeader.Substring(JwtBearerDefaults.AuthenticationScheme.Length + 1);
                        if (authorizeHandler.TokenExpired(token)) //TODO while token expired time is less than 1 min, refresh token
                        {
                            context.Response.StatusCode = 401;
                            throw new AuthenticationException("Token expired");
                        }
                    }

                    await _next(context);
                }
                else
                {
                    await _next(context);
                }
            }
            catch (Exception exception)
            {
                await HandleErrorAsync(context, exception);
            }
        }
Example #2
0
 public AuthController(IGate gate, IJwtAuthorizeHandler authorizeHandler)
 {
     _gate             = gate;
     _authorizeHandler = authorizeHandler;
 }
Example #3
0
 public AuthController(IJwtAuthorizeHandler authorizeHandler)
 {
     _authorizeHandler = authorizeHandler;
 }