WebService target CookieContainer

3 messages Options
Embed this post
Permalink
Matthew

WebService target CookieContainer

Reply Threaded More More options
Print post
Permalink
I am using webService target for auditing users from my winform applications, which is based on massive webService communication with my central server. At first, they login through a webService and if successfull, then create Authentication cookie through FormsAuthentication.SetAuthCookie(userName, false); This is stored in my application in static CookieContainer variable, so every next webService request is done in the same authorized user context and I can get the user in webservice.

What I need is to pass the CookieContainer used by my application to NLog webService "engine" calling my Audit web method, so inside of that, I can get the user for which I am writing the audit.

I could possibly send the user name by the webMethod each time in audit, but this wouldn't be the prefered way. If you have any suggestions or tips, how to achieve this, I would be very thankfull.

If you need any further explanation, please do not hesitate to write.
Matthew

Re: WebService target CookieContainer

Reply Threaded More More options
Print post
Permalink
(This post was updated on )
Ok, this was the easy one. The solution is to add private property System.Net.CookieContainer _cookieContainer to NLog-1.0-Refresh\src\NLog\Targets\WebService.cs file (around line 117) and publish it through the public CookieContainer property, so you can insert your own CookieContainer. After that, in each method (tested only for InvokeSoap11(object[] parameters) ) you have to add this 2 lines to set the CookieContainer for WebRequest object, through which the actual webMethod call is made (somewhere around line 188):

            if (_cookieContainer != null)
                request.CookieContainer = _cookieContainer;

then build the NLog project and use the new dll in your project insted of the original one.

The last step is to set your CookieContainer to the WebServiceTarget in the method, where you programaticaly setup the Loggers & targets. This can be done only programaticaly. Example of mine:

public LoggerInit(CookieContainer cookies, string _webServiceServerUrl) {
   LoggingConfiguration config = new LoggingConfiguration();
   // other Logger config set ....

   // create WebServiceTarget
                WebServiceTarget auditFile = new WebServiceTarget();
                config.AddTarget("audit", auditFile);

                auditFile.MethodName = "Audit"; // you have to set parameters for the method of course, shortened here
                auditFile.Url = _webServiceServerUrl;
                auditFile.Namespace = "MyApp.Web.WebServices";
                auditFile.Protocol = WebServiceTarget.WebServiceProtocol.Soap11;

                // here insert your cookieContainer used elsewhere in your application for your webService communication
                auditFile.WebServiceCookieContainer = cookies;

  // another config stuff ...
}


Jaroslaw Kowalski

Re: WebService target CookieContainer

Reply Threaded More More options
Print post
Permalink
Thanks Matthew. Adding CookieContainer to WebService target looks like a useful feature. Please file a bug on http://nlog.codeplex.com so that this functionality makes it into NLog v2.
 
Jarek

 
On Tue, Oct 20, 2009 at 4:19 PM, Matthew [via NLog Forum] <[hidden email]> wrote:
Ok, this was the easy one. The solution is to add private property System.Net.CookieContainer _cookieContainer to NLog-1.0-Refresh\src\NLog\Targets\WebService.cs file (around line 117) and publish it through the public CookieContainer property, so you can insert your own CookieContainer. After that, in each method (tested only for InvokeSoap11(object[] parameters) ) you have to add this 2 lines to set the CookieContainer for WebRequest object, through which the actual call is made (somewhere around line 188):

            if (_cookieContainer != null)
                request.CookieContainer = _cookieContainer;

then build the NLog project and use the new dll in your project insted of the original one.

The last step is to set your CookieContainer to the WebServiceTarget in the method, where you programaticaly setup the Loggers & targets. This can be done only programaticaly. Example of mine:

public Init(CookieContainer cookies) {
   // other Logger config set ....

   // create WebServiceTarget
                WebServiceTarget auditFile = new WebServiceTarget();

                auditFile.MethodName = "Audit"; // you have to set parameters for the method of course, shortened here
                auditFile.Url = _webServiceServerUrl;
                auditFile.Namespace = "Logos.Sepo.Web.WebServices";
                auditFile.Protocol = WebServiceTarget.WebServiceProtocol.Soap11;

                // here insert your cookieContainer used elsewhere in your application for your webService communication
                auditFile.WebServiceCookieContainer = cookies;

  // another config stuff ...
}