ServiceStack: Accessing the HttpRequest in a selfhosted application

后端 未结 1 1046
半阙折子戏
半阙折子戏 2020-12-19 06:21

I currently have an IIS hosted application that I would like to switch over to use the self-hosted method.

But I\'m having difficulty accessing the session so I can

相关标签:
1条回答
  • 2020-12-19 06:29

    For ServiceStack 3, you can share request data via the HostContext.Instance.Items Dictionary. For ServiceStack 4, you should use the HostContext.RequestContext.Items Dictionary.

    For example, add a request filter in your app host configuration to save the value:

    // Put the session into the hostcontext.
    RequestFilters.Add((req, res, requestDto) =>
    {
      HostContext.Instance.Items.Add("Session", req.GetSession());
    });
    

    Then in your authentication token class pull it back out:

    public string GetCurrentAuthToken()
    {
      var session = HostContext.Instance.Items["Session"] as AuthUserSession;
    
       if (session != null)
       {
         return session.UserName;
       }
    
       throw new Exception("No attached session found.");
    }
    
    0 讨论(0)
提交回复
热议问题