Search code examples
wcfauthenticationwcf-security

WCF Authentication Service Proxy - CookieContainer not available


I have enabled the ASP.Net authentication service, as recommended by msdn. I am then attempting to use the service via a console app or winforms app (by adding a service reference to my local WCF service). I am doing custom authentication and transport security (so I am handling the AuthenticationService.Authenticating event in my Global.asax which works fine).

The authentication itself works fine, but the proxy created by adding the Service Reference does not include the CookieContainer property. This is obviously a problem when I try to pass the cookie token to subsequent services which require authentication.

Also, in the following client code, the IsLoggedIn() returns false, I'm guessing this is related to no cookie container being present.

ServiceReference1.AuthenticationServiceClient client = 
                  new ServiceReference1.AuthenticationServiceClient();
bool isLoggedIn = client.Login("test", "test", "", true); //returns TRUE
bool check = client.IsLoggedIn(); //returns FALSE

Here is my web.config on the service:

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
  <system.web.extensions>
    <scripting>
      <webServices>
        <authenticationService enabled="true"
           requireSSL = "false"/>
      </webServices>
    </scripting>
  </system.web.extensions>
  <system.serviceModel>
    <services>
      <service name="System.Web.ApplicationServices.AuthenticationService"
          behaviorConfiguration="AuthenticationServiceTypeBehaviors">
        <endpoint contract="System.Web.ApplicationServices.AuthenticationService"
          binding="basicHttpBinding"
          bindingConfiguration="userHttps"
          bindingNamespace="http://asp.net/ApplicationServices/v200"/>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="userHttps" allowCookies="true">
          <!--<security mode="Transport" />-->
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="AuthenticationServiceTypeBehaviors">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>
</configuration>

EDIT: Something else I should add, I did a Fiddler session of the service calling the Login method, and the cookie is being set and sent back to the client. But what am I supposed to do with no CookieContainer?


Solution

  • Apparently, when adding a Service Reference (.Net 3.5+) to a WCF service on a client, the proxy class derives from System.ServiceModel.ClientBase. This class does not have a CookieContainer property (because the ClientBase supports non-HTTP protocols that have no concept of cookies).

    http://netpl.blogspot.com/2011/11/managing-cookies-in-wcf-client.html

    I could add a Web Reference instead, which would use the .Net 2.0 proxy class (and has CookieContainer property exposed) http://msdn.microsoft.com/en-us/library/bb628649.aspx. But I will most likely revisit my approach entirely and use custom headers and service behaviors to accomplish my goal.