Note that there are some explanatory texts on larger screens.

plurals
  1. POServiceStack: Accessing the HttpRequest in a selfhosted application
    primarykey
    data
    text
    <p>I currently have an IIS hosted application that I would like to switch over to use the self-hosted method.</p> <p>But I'm having difficulty accessing the session so I can retrieve the current users username.</p> <p>This is the code I used when hosting under IIS which worked perfectly:</p> <pre><code>/// &lt;summary&gt; /// A basic wrapper for the service stack session, to allow access to it lower down in the DAL layer without tying us to servicestack. /// &lt;/summary&gt; public class ServiceStackAuthTokenService : IAuthTokenService { /// &lt;summary&gt; /// GetCurrentAuthToken. /// &lt;/summary&gt; /// &lt;returns&gt;A string representing the users auth name.&lt;/returns&gt; 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&lt;AuthService&gt;(); 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"; } } } </code></pre> <p>This is added to the app host with the following code::</p> <pre><code>container.RegisterAutoWiredAs&lt;ServiceStackAuthTokenService, IAuthTokenService&gt;() </code></pre> <p>When running self-hosted the HttpContext.Current is null, how do I access the request under a self-hosted application?</p> <p>Thanks!</p> <p><strong>Update</strong> Additional things I have tried:</p> <p>as per an post here: <a href="https://groups.google.com/forum/#!msg/servicestack/jnX8UwRWN8A/_XWzTGbnuHgJ">https://groups.google.com/forum/#!msg/servicestack/jnX8UwRWN8A/_XWzTGbnuHgJ</a></p> <p>It was suggested to use: </p> <p>container.Register>(c => AuthService.CurrentSessionFactory);</p> <p>This just returns a newed IAuthSession.</p> <p>What the user in that post is doing is exactly what I'm trying to achieve. </p> <p>In the last post Mythz says: </p> <p>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.</p> <p>But I still cant see a way to access the IHttpRequest.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload