Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

session - ServiceStack: Accessing the HttpRequest in a selfhosted application

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 retrieve the current users username.

This is the code I used when hosting under IIS which worked perfectly:

/// <summary>
/// A basic wrapper for the service stack session, to allow access to it lower down in the DAL layer without tying us to servicestack.
/// </summary>
public class ServiceStackAuthTokenService : IAuthTokenService
{
    /// <summary>
    /// GetCurrentAuthToken.
    /// </summary>
    /// <returns>A string representing the users auth name.</returns>
    public string GetCurrentAuthToken()
    {
        // Grab the current request.
        var req = HttpContext.Current.Request.ToRequest();
        var res = HttpContext.Current.Response.ToResponse();

        // Fetch the authentication service.
        var authService = EndpointHost.AppHost.TryResolve<AuthService>();
        authService.RequestContext = new HttpRequestContext(req, res, null);

        // Grab the session.
        var session = authService.GetSession(false);

        // Return the username.
        return session.UserName;
    }

    public string UserPropertyName
    {
        get { return "UserName"; }
    }
}

This is added to the app host with the following code::

container.RegisterAutoWiredAs<ServiceStackAuthTokenService, IAuthTokenService>()

When running self-hosted the HttpContext.Current is null, how do I access the request under a self-hosted application?

Thanks!

Update Additional things I have tried:

as per an post here: https://groups.google.com/forum/#!msg/servicestack/jnX8UwRWN8A/_XWzTGbnuHgJ

It was suggested to use:

container.Register>(c => AuthService.CurrentSessionFactory);

This just returns a newed IAuthSession.

What the user in that post is doing is exactly what I'm trying to achieve.

In the last post Mythz says:

Just to be clear, in order to form the Session Key that references the Users session you need either the ss-id or ss-pid cookies (as determined by ss-opts). You can get cookies off the IHttpRequest object or otherwise in ASP.NET the HttpContext.Current.Request singleton, so whatever IAuthUserSession factory you inject needs to take something that can give it the cookies, i.e. either an IRequestContext, IHttpRequest, IService, etc.

But I still cant see a way to access the IHttpRequest.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...