简体   繁体   English

JsonServiceClient不使用cookie放置在cookiecontainer中

[英]JsonServiceClient not using cookie placed in cookiecontainer

I've posted a couple of questions previously to get where I am now: 我之前发布了几个问题,以便了解我现在所处的位置:

Reconnecting to Servicestack session in an asp.net MVC4 application 重新连接到asp.net MVC4应用程序中的Servicestack会话

and

nullreference exception when adding session cookie to ServiceStack 向ServiceStack添加会话cookie时出现nullreference异常

Quick background on the app: This is an asp.net MVC application accessing data on a remote servicestack installation. 应用程序的快速背景:这是一个访问远程服务堆栈安装数据的asp.net MVC应用程序。

At this point I am successfully authenticating with SS, saving the session key in a cookie, and inserting that cookie into the CookieContainer of a new JsonServiceClient instance. 此时,我使用SS成功进行身份验证,将会话密钥保存在cookie中,并将该cookie插入到新JsonServiceClient实例的CookieContainer中。

However when I try to grab some data via the new JsonServiceClient instance: 但是,当我尝试通过新的JsonServiceClient实例获取一些数据时:

CallList = client.Get(new ServiceCallRequest()).Result;

The remote ServiceStack instance seems to be redirecting me to the default ASP login area (/Auth/login or something similar). 远程ServiceStack实例似乎将我重定向到默认的ASP登录区域(/ Auth / login或类似的东西)。 This redirect in itself isn't a problem, but it does seem to indicate that the client isn't using the SS session already established on the remote machine. 此重定向本身不是问题,但它似乎表明客户端未使用已在远程计算机上建立的SS会话。

This is the code that is actually inserting the cookie into the client cookie container and calling for a list of objects: 这是实际将cookie插入客户端cookie容器并调用对象列表的代码:

    public List<ServiceCallModel> LoadList()
    {
        try
        {
            var cookie = HttpContext.Request.Cookies.Get(SessionFeature.PermanentSessionId);
            var client = new JsonServiceClient([api address]);
            cookie.Domain = ".domain.com"; 
            var cookie1 = new Cookie(SessionFeature.PermanentSessionId, cookie.Value);
            cookie1.Domain = ".domain.com"; 
            client.CookieContainer.Add(cookie1);

            List<ServiceCallModel> CallList = new List<ServiceCallModel>();
            CallList = client.Get(new ServiceCallRequest()).Result;
            return CallList;
        }
        catch (Exception ex)
        {
            return new List<ServiceCallModel>();
        }
    }

I can verify that this remote resource works with a monotouch Android application using the C# client. 我可以使用C#客户端验证此远程资源是否与单触式Android应用程序一起使用。 The only difference of course is that the Android client is persistent; 唯一的区别当然是Android客户端是持久的; the one in question here isn't. 这里有问题的不是。

The above example always returns a WebServiceException ("Not Found") (Which I assume is actually a 401/403 that has been redirected annoyingly by ASP). 上面的例子总是返回一个WebServiceException(“Not Found”)(我假设它实际上是一个被ASP恼人地重定向的401/403)。

Does this seem reasonable, or am I missing/misunderstanding some functionality of the JsonServiceClient/ServiceStack? 这看起来是否合理,或者我是否遗漏/误解了JsonServiceClient / ServiceStack的某些功能?

Thanks much 非常感谢

Update 更新

Using Fiddler, I can confirm that the cookie is being stored in the browser and sent back to the MVC application in the request header: 使用Fiddler,我可以确认cookie存储在浏览器中并发送回请求头中的MVC应用程序:

GET / HTTP/1.1
Host: [web app address]
Connection: keep-alive
Cache-Control: max-age=0    
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)                         
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: ASP.NET_SessionId=3457C511FECBECCD3C055C21;.MONOAUTH=PzE6iBuLIbv1evgACfpUwpC1D7opCANroPDQN/mXHNvGAgkjqq04Tdd8EnnGTL7y3lWYWY4+GWaXGDT0Fm7+eJxRdpy    LJMaUQ6BiYmb3dxRi1B3f/qkmPMbFIoC7vC9M; ss-pid=lzJ+o9vG/7F3YZ9JNN2F

At this point I am trying to find out of that same ss-pid value is then making it back to the API server in a request header. 此时,我试图找出相同的ss-pid值,然后将其返回到请求标头中的API服务器。

Update 2 更新2

Using tcpdump I was able to see that the ss-pid value is in fact making it all the way back to the API server (The "remote servicestack instance"). 使用tcpdump我能够看到ss-pid值实际上使它一直回到API服务器(“远程服务栈实例”)。 So now I think I need to troubleshoot that, not the client. 所以现在我认为我需要排除故障,而不是客户端。 Any thoughts? 有什么想法吗?

Snippet of tcpdump output : tcpdump输出的片段

    0x0090:  6e65 740d 0a43 6f6f 6b69 653a 2073 732d  net..Cookie:.ss-
    0x00a0:  7069 643d 6c7a 4a2b 6f39 7647 2f37 4633  pid=lzJ+o9vG/7F3
    0x00b0:  595a 394a 4e4e 3246 0d0a 4163 6365 7074  YZ9JNN2F..Accept

I know that the ss-pid values are different in each part of this post. 我知道这篇文章的每个部分的ss-pid值都不同。 They were obtained at different times 它们是在不同时间获得的

Update 3 更新3

I've also changed the LogFormat in the vhost config file to spit out the value for the cookie called "ss-pid" (At the end of the log entry). 我还更改了vhost配置文件中的LogFormat,以吐出名为“ss-pid”的cookie的值(在日志条目的末尾)。

The resulting logs on the ServiceStack remote API server looks like this: ServiceStack远程API服务器上生成的日志如下所示:

172.16.0.17 - - [08/Oct/2013:12:26:52 -0400] "GET /sc/0 HTTP/1.1" 500 3082 "-" "-" "HFMtFpPQkpE0Br6/fEFg"
172.16.0.17 - - [08/Oct/2013:12:27:06 -0400] "GET /sc/0 HTTP/1.1" 302 394 "-" "-" "HFMtFpPQkpE0Br6/fEFg"
172.16.0.17 - - [08/Oct/2013:12:27:07 -0400] "GET /login.aspx?ReturnUrl=%2fsc%2f0 HTTP/1.1" 404 451 "-" "-" "HFMtFpPQkpE0Br6/fEFg"

This "500" status on the first request sticks out. 第一个请求中的“500”状态突然显示出来。 I will be investigating this now. 我现在要调查这个。

Update 4 更新4

The 500 status seems to be a case of Microsoft.Web.Infrastructure.dll being included in the bin directory. 500状态似乎是bin.目录中包含Microsoft.Web.Infrastructure.dll的情况。 Deleted that to resolve the 500 response, but this did not fix the overall problem. 删除了解决500响应,但这并没有解决整体问题。

ServiceStack places two Session cookies in the Request, 'ss-id' and 'ss-pid'. ServiceStack在Request,'ss-id'和'ss-pid'中放置两个Session cookie。 This line in your code... 你的代码中的这一行......

var cookie = HttpContext.Request.Cookies.Get(SessionFeature.PermanentSessionId);

will grab the 'ss-pid' cookie. 将获取'ss-pid'cookie。 But, you probably want the grab the 'ss-id' cookie which is the cookie for your current authenticated session. 但是,您可能希望获取'ss-id'cookie,这是您当前经过身份验证的会话的cookie。

var cookie = HttpContext.Request.Cookies.Get(SessionFeature.SessionId);

Take a look here for more information on ServiceStack Sessions. 有关ServiceStack会话的更多信息,请查看此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM