如何将Cookie容器与RestSharp和ASP.NET会话一起使用? [英] How do I use the cookie container with RestSharp and ASP.NET sessions?

查看:54
本文介绍了如何将Cookie容器与RestSharp和ASP.NET会话一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在控制器上调用身份验证操作,如果操作成功,则将经过身份验证的用户详细信息存储在会话中.

I'd like to be able to call an authentication action on a controller and if it succeeds, store the authenticated user details in the session.

但是,由于我将RestSharp用作独立客户端,因此我不确定如何将请求保留在会话中.我需要以某种方式从成功的授权服务器上取回密钥,然后对于以后的每个呼叫,将密钥与会话中存储的密钥进行核对.

However, I'm not sure how to keep the requests inside the session as I'm using RestSharp as a detached client. I need to somehow get a key back from the server on successful authorisation and then for each future call, check the key with that stored in the session.

如何确保RestSharp中的RestClient发送所有以后的请求以及正确设置的cookie,以便在服务调用中可以正确检索会话变量?

How do I ensure the RestClient in RestSharp sends all future requests with the cookie set correctly so inside service calls, the session variable can be retrieved correctly?

我一直在用HttpFactory查看cookie容器,但是似乎没有关于此的任何文档.

I've been looking at the cookie container with HttpFactory but there doesn't seem to be any documentation on this anywhere.

推荐答案

我最后解决了这个问题.基本上创建一个cookie容器,然后将响应中的会话cookie添加到cookie容器中.这样,以后所有的请求都将包含此Cookie.

I worked this out in the end. Basically create a cookie container, then add the session cookie from the response into the cookie container. All future requests will then contain this cookie.

 var sessionCookie = response.Cookies.SingleOrDefault(x => x.Name == "ASP.NET_SessionId");
 if (sessionCookie != null)
 {
    _cookieJar.Add(new Cookie(sessionCookie.Name, sessionCookie.Value, sessionCookie.Path, sessionCookie.Domain));
 }

这篇关于如何将Cookie容器与RestSharp和ASP.NET会话一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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