Create a C # proxy for an existing web service for processing cookies

I have a third-party tool (T) through which I need to use a web service. The problem is that the service requires the client to be able to process cookies, but T cannot.

The service requires a call to the Login method, which sets a cookie that should be used with future calls.

So, I made a temporary web service that processes cookies and returns SessionId, which is Guid for T, which T can use for subsequent calls.

I use a persistent dictionary

private Dictionary<Guid, CookieContainer> _cookieJar 

made permanent using the ServiceBehavior attribute, for example, decorating a service class as follows:

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class ServiceClass: IServiceClass 

to store the cookie and return sessionId when calling Login. When T makes subsequent calls with sessionId, I forward these requests with the appropriate CookieContainer from the dictionary attached to it using:

 request.CookieContainer = _cookieJar[new Guid(sessionId)]; 

request is HttpWebRequest

Like the Login method, there is a Logout method that actually expires in the cookie, and I use this call to delete an entry from the dictionary. For cases when the Logout call has not been completed, I could save the time during which the Cookie was created, so I can check and delete old Cookie containers from the dictionary when and when new ones are added.

Is there a better way or recommended approach for creating such a proxy?

+5
source share

Source: https://habr.com/ru/post/1266049/


All Articles