title: ServiceStack user session not found when using sessionId in client Headers or Cookies
body: <p>I am using ServiceStack v4 with custom Authentication. This is setup and working correctly. I can call the /auth service and get a returned AuthorizationResponse with unique SessionId. </p>
  
  <p>I also have swagger-ui plugin setup. Using it, I can authenticate via /auth and then call one of my other services which require authentication without issue.</p>
  
  <p>Now, from a secondary MVC application using the c# JsonServiceClient I can again successfully make a call to /auth and then secured services using the same client object. However, if I dispose of that client (after saving the unique sessionId to a cookie), then later create a new client, and either add the sessionId as a Cookie or via headers using x-ss-pid as documented, calling a services returns 401. If I call a non-secure service, but then try to access the unique user session, it returns a new session. </p>
  
  <p>If I look at the request headers in that service, the cookie or Header is clearly set with the sessionId. The sessionId also exists in the sessionCache. The problem seems to be that the code which tries to get the session from the request isn't finding it.</p>
  
  <p>To be more specific, it appears that ServiceExtensions.GetSessionId is looking at the HostContext and not the calling Request. I'm not sure why. Perhaps I misunderstand something along the way here. </p>
  
  <p>If I directly try and fetch my expected session with the following code it's found without issue.</p>
  
  <pre><code>  var req = base.Request;
    var sessionId = req.GetHeader("X-" + SessionFeature.PermanentSessionId);
  
    var sessionKey = SessionFeature.GetSessionKey(sessionId);
    var session = (sessionKey != null ? Cache.Get&lt;IAuthSession&gt;(sessionKey) : null)?? SessionFeature.CreateNewSession(req, sessionId);
  </code></pre>
  
  <p>So, am I missing something obvious here? Or maybe not so obvious in creating my secondary client?</p>
  
  <p><strong>Sample code of client calls</strong></p>
  
  <p>Here is my authorization code. It's contained in a Controller class. This is just the relevant parts.</p>
  
  <pre><code>using (var client = new JsonServiceClient(WebHelper.BuildApiUrl(Request)))
  {
      try
      {
          loginResult = client.Post(new Authenticate()
                         {
                             UserName = model.Email,
                             Password = model.Password,
                             RememberMe = model.RememberMe
                         });
  
          Response.SetCookie(new HttpCookie(SessionFeature.PermanentSessionId, loginResult.SessionId));
  
          return true;
       }
  }
  </code></pre>
  
  <p><strong>Here is my secondary client setup and service call, contained in it's own controller class in another area of the MVC application</strong></p>
  
  <pre><code>using (var client = new JsonServiceClient(WebHelper.BuildApiUrl(Request)))
  {
      var cCookie = HttpContext.Request.Cookies.Get(SessionFeature.PermanentSessionId);
      if (cCookie != null)
      {
          client.Headers.Add("X-" + SessionFeature.PermanentSessionId, cCookie.Value);
          client.Headers.Add("X-" + SessionFeature.SessionOptionsKey, "perm");
  
      }
  
      response = client.Get(new SubscriptionStatusRequest());
  }
  </code></pre>
  
  <p><strong>Additional Update</strong></p>
  
  <p>During the Authenticate process the following function is called from HttpRequestExtensions with the name = SessionFeature.PermanentSessionId</p>
  
  <pre><code>public static class HttpRequestExtensions
  {
      /// &lt;summary&gt;
      /// Gets string value from Items[name] then Cookies[name] if exists.
      /// Useful when *first* setting the users response cookie in the request filter.
      /// To access the value for this initial request you need to set it in Items[].
      /// &lt;/summary&gt;
      /// &lt;returns&gt;string value or null if it doesn't exist&lt;/returns&gt;
      public static string GetItemOrCookie(this IRequest httpReq, string name)
      {
          object value;
          if (httpReq.Items.TryGetValue(name, out value)) return value.ToString();
  
          Cookie cookie;
          if (httpReq.Cookies.TryGetValue(name, out cookie)) return cookie.Value;
  
          return null;
      }
  </code></pre>
  
  <p>Now what occurs is the httpReq.Items contains a SessionFeature.PermanentSessionId value, but I have no clue why and where this gets set. I don't even understand at this point what the Items container is on the IRequest. The code thus never gets to the functionality to check my cookies or headers</p>