Cookies in wcf service OperationContract

1.2k Views Asked by At

Is it possible to read cookies in the OperationContract of a wcf service? I am trying to read a cookie value in the contract method, but its always empty. If I read the same cookie from a .aspx page, the value is present. Any ideas?

2

There are 2 best solutions below

0
Kirk Woll On BEST ANSWER

How are you hosting them? WCF is intended to be host-neutral -- i.e. your services should still work when hosted outside of IIS. However, there is a compatibilty mode which might help you:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>

The default value is false and disables most ASP.NET features like HttpContext.Current.

0
dumbledad On

The BasicHttpBinding.AllowCookies Property may fix this, as mentioned at the start of Enrico's blog post on Managing shared cookies in WCF (referenced here). The post includes the web.config fragment:

<system.ServiceModel>
    <bindings>
        <basicHttpBinding allowCookies="true">
    </bindings>
    <client>
        <endpoint address="http://localhost/myservice"
                  binding="basicHttpBinding"
                  contract="IMyService" />
    </client>
</system.ServiceModel>

but no code fragment using it (the blog post has code for more complex solutions using the same cookies with different web services).

======== EDIT ==========

Or perhaps even allowCookies=false