ServiceStack:访问的Htt prequest在selfhosted应用 [英] ServiceStack: Accessing the HttpRequest in a selfhosted application

查看:176
本文介绍了ServiceStack:访问的Htt prequest在selfhosted应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一个IIS托管应用,我想切换到使用自托管的方法。

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.

这是code IIS下这工作完全托管当我用:

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

这是添加到应用程序主机具有以下code:

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

container.RegisterAutoWiredAs<ServiceStackAuthTokenService, IAuthTokenService>()

在运行自托管的HttpContext.Current为null,我怎么一个自托管的应用程序中访问请求?

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

谢谢!

更新
其他的事情我已经试过:

Update Additional things I have tried:

根据这里的帖子:<一href=\"https://groups.google.com/forum/#!msg/servicestack/jnX8UwRWN8A/_XWzTGbnuHgJ\">https://groups.google.com/forum/#!msg/servicestack/jnX8UwRWN8A/_XWzTGbnuHgJ

有人建议使用:

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

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

这只是返回newed IAuthSession。

This just returns a newed IAuthSession.

什么在该职位是做用户正是我想要的目的。

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

在帖子的最后Mythz说:

In the last post Mythz says:

只是要清楚,以形成会话密钥引用用户会话您需要使用的SS-ID或SS-PID饼干(如SS-OPTS确定)。
你可以下车IHtt prequest对象或以其他方式在ASP.NET中HttpContext.Current.Request单饼干,所以无论IAuthUserSession出厂你注入需要采取的东西,可以给它饼干,即要么是IRequestContext,IHtt prequest,IService等。

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.

但我仍然无法看到的方式来访问IHtt prequest。

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

推荐答案

好了后多解决此挖似乎工作:

Well after much digging around this seems to work:

首先,在您的应用程序的主机配置中添加一个请求筛选:

Firstly add a request filter in your app host configuration:

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

然后在我的认证令牌类将其背出来:

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

如果Mythz大约将是很好,如果他能确认这是正确的事情做在自托管的应用程序;)

If Mythz is around would be nice if he could confirm this is the right thing to be doing in a self hosted application ;)

这篇关于ServiceStack:访问的Htt prequest在selfhosted应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆