开发者

.NET Compact Framework, WCF service, compression and DIGEST authentication

开发者 https://www.devze.com 2023-03-13 06:50 出处:网络
I\'m trying to put a number of features together, which is proving increasingly difficult due to the limitations of the .NET Compact Framework.

I'm trying to put a number of features together, which is proving increasingly difficult due to the limitations of the .NET Compact Framework.

Specifically, I've got a WCF service and I'm writing a mobile device client for it. The catch? I want to use some sort of data compression (due to a very slow modem connected to said device) and HTTP DIGEST authentication (which is already in place on the site hosting the WCF service).

I've followed this blog entry to get the compression and generated code needed for the WCF service client.

I am, however, struggling with the HTTP DIGEST. I've no idea how to add this functionality.

Previously I didn't use compression and as such I connected to the WCF service using SOAP, using a simple WebService reference, and to add HTTP DIGEST I had to override the GetWebRequest method and add the required headers manually. This time around the generated classes seem to give very little lee开发者_开发技巧way and there isn't much I can override. Also, all security or authentication parameters seem to be designed for SSL, rather than such basic authentication schemes.

To summarize: how can I create a WCF client using compression and HTTP DIGEST authentication using .NET Compact Framework?

EDIT: Here's the code I've currently got:

        System.ServiceModel.Channels.CustomBinding customBinding = new System.ServiceModel.Channels.CustomBinding();
        CompressionMessageEncodingBindingElement compressionBindingElement = new CompressionMessageEncodingBindingElement();
        customBinding.Elements.Add(compressionBindingElement);
        HttpTransportBindingElement httpBindingElement = new HttpTransportBindingElement();
        customBinding.Elements.Add(httpBindingElement);
        EndpointAddress endPoint = new EndpointAddress("http://localhost:5100/Service.svc");
        ServiceClient client = new ServiceClient(customBinding, endPoint);

I suspect I somehow need to add the HTTP DIGEST functionality to the CustomBinding class, but I don't know how.

I suspect I should also note, that while I am using .NET Compact Framework 3.5, I am creating a Windows CE application. As such, I didn't bother downloading Windows Mobile 6 SDKs. If these SDKs add more functionality which can be used in Window CE applications and are required for the HTTP DIGEST to work, please let me know.


We ended up disabling the DIGEST authentication for devices running .NET CF. It's not as safe, but we figured the data send and retrieved by the devices running .NET CF in our case isn't THAT sensitive, so all we really need to do is validate it.


If the client is running on the .NET Compact Framework 3.5, you can use WCF to invoke the service and use the built-in support for HTTP Digest authentication without requiring SSL.

Here's how to programmatically configure a WCF client to use Digest authentication with the BasicHttpBinding:

var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Digest;
var endpoint = new EndpointAddress("http://server/myservice");
var client = new MyServiceClient(binding, endpoint);

// We have to set the actual credentials on the client proxy object
// before invoking the service:
client.ClientCredentials.HttpDigest.ClientCredential.UserName = "me";
client.ClientCredentials.HttpDigest.ClientCredential.Password = "password";

try
{
    client.MyServiceOperation();
    client.Close();
}
catch
{
    client.Abort();
}

Related resources:

  • The WCF subset supported by the .NET Compact Framework 3.5
  • WCF Guidance for Mobile Developers (see page 66 for HTTP Digest Authentication)


The only way to achieve this is to use HttpWebRequest (manually) and specify ClientCredentials, instead of the generated classes from NetCFSvcUtil which does not support authentication. The only WS-Security specification it supports on CF with WCF is to effectively use message security with a Mutual Certificate exchange. (Which by the way has a memory leak which a colleage and I found: http://connect.microsoft.com/VisualStudio/feedback/details/727247/native-memory-leak-in-wcf-proxy-client-with-mutual-certificate-security-in-net-compact-framework-3-5-on-windows-ce-6-0)

Of note, the generated CFClientBase also has a memory leak which can be worked around, see: http://geekswithblogs.net/GruffCode/archive/2013/03/31/memory-leak-in-cfclientbaselttgt-service-proxy-for-compact-framework-.net.aspx

For reference: The WCF subset supported by NetCF: http://blogs.msdn.com/b/andrewarnottms/archive/2007/08/21/the-wcf-subset-supported-by-netcf.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号