如何将Cookie容器与RestSharp和ASP.NET会话一起使用?

编程入门 行业动态 更新时间:2024-10-28 19:34:15
本文介绍了如何将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会话一起使用?

本文发布于:2023-11-15 13:24:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1594825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   容器   Cookie   NET   RestSharp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!