Session null in RequestLogger when using JWT access token

I have created a custom RequestLogger

public class CustomRequestLogger : InMemoryRollingRequestLogger
  {

    public override void Log(IRequest request, object requestDto, object response, TimeSpan requestDuration)
    {
      base.Log(request, requestDto, response, requestDuration);
      var requestType = requestDto != null ? requestDto.GetType() : null;
      var appSettings = HostContext.AppSettings;
      if (ExcludeRequestType(requestType))
        return;

      var entry = CreateEntry(request, requestDto, response, requestDuration, requestType);
      if (entry.Session == null)
      {
        // this is always true
      }

    }
  }

I authenticate with my service by adding a header “Authorization” to the request with value “Bearer [the token]”.

The service authenticates fine, and the session is populated and accessible in the services, however, the value of entry.Session is always null.

When I authenticate using the Session Feature, the Session is filled.

Is there a way to retrieve the session when using the JwtAuthProvider?

Have you enabled EnableSessionTracking?

Plugins.Add(new RequestLogsFeature {
    EnableSessionTracking = true
});

Yes, I have.

If I authenticate using the Credentials AuthProvider, the session is correctly filled.

I’ve created new integrated tests showing JWT Sessions are being logged in RequestLogEntry for JWT Requests in this commit.

Thanks for creating the test.
However, I think the difference between this test and my scenario is that you are creating the BearerToken in the test by calling the Authenticate Post before setting the BearerToken in the header. This results in the session being populated.

BearerToken = GetClient().Post(new Authenticate()).BearerToken

Hower, my use case is is the following:

I am calling Authenticate to retrieve the bearer token.

In subsequent calls, I am adding this token as Authorzisation header "Bearer " without calling the Authenticate service.

In this case the Session is always null.

This is what populating BearerToken on the Service Client does, i.e. adds it to the “Authorization” HTTP Header:

Can you modify my integration test to include a failing test that shows the issue.

I tried, but the test was passing. As it turned out, no issue in the ServiceStack code.

There were two issues in my code:

  1. The first call was a preflight check for the CORS feature, these requests are not authenticated and can be ignored in teh logger.

  2. I did set “EnableSessionTracking” to true, but directly on the RequestLogger:

     Plugins.Add(new RequestLogsFeature
           {
             RequestLogger = new RequestLogger()
             {
               EnableResponseTracking = true,
               EnableErrorTracking = true,
               EnableSessionTracking = true,
               EnableRequestBodyTracking = true,
             },
           });
    

    where thiis must be:

      Plugins.Add(new RequestLogsFeature
           {
             EnableErrorTracking = true,
             EnableRequestBodyTracking = true,
             EnableSessionTracking = true,
             EnableResponseTracking = true,
             RequestLogger = new RequestLogger()
           });
    

Almost the same :smile:

Thanks for your support