How to Make iPhone Web Service Calls to a WCF Service Using Certificate Authentication in iOS

Introduction to iPhone Web Service Calls with WCF Services and Certificate Authentication

As a .NET developer, you’re likely familiar with building web services using Windows Communication Foundation (WCF). However, when it comes to integrating these services with iOS applications, things can get complex. In this article, we’ll explore how to make iPhone web service calls to a WCF service using certificate authentication.

Prerequisites: Setting up the WCF Service

Before we dive into the iPhone code, let’s set up our WCF service. We’ll use Visual Studio 2012 or later and create a new WCF project. For this example, we’ll assume you have already created a WCF service with certificate authentication.

Step 1: Generate the Certificate

If you haven’t already generated a certificate for your WCF service, follow these steps:

  1. Open the Visual Studio “Certificate Manager” tool.
  2. Create a new self-signed certificate or import an existing one.
  3. Set the expiration date to a reasonable value (e.g., 1 year).

This certificate will be used for authentication purposes in our WCF service.

Step 2: Configure the WCF Service

Update your web.config file with the following configuration:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_WCFService" closeTimeout="00:01:00">
                <security mode="Transport">
                    <transport clientCredentialType="Certificate"/>
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
</system.serviceModel>

This configuration enables basic HTTP binding with Transport security, using the certificate as the client credential type.

iPhone Code: Making the Web Service Call

Now it’s time to write the iPhone code that makes the web service call. We’ll use Objective-C and the NSURLConnection class to make the request.

Step 1: Create an iOS Project

Create a new iOS project in Xcode, choosing “Single View App” as the template.

Step 2: Import necessary frameworks

In your Info.plist file, add the following frameworks:

  • Security
  • SystemConfiguration

Also, import the NSURLConnection framework in your Objective-C file:

#import <Foundation/Foundation.h>
#import <URLConnection/NSURLConnection.h>

Step 3: Define the WCF Service URL and Certificate

Create a new class, e.g., WCFServiceRequest, to store the WCF service URL and certificate:

@interface WCFServiceRequest : NSObject

@property (nonatomic, copy) NSString *wcfServiceUrl;
@property (nonatomic, copy) NSData *certificate;

@end

Step 4: Implement the Connection Delegate

Create a new class, e.g., WCFServiceDelegate, to implement the NSURLConnectionDelegate protocol:

@interface WCFServiceDelegate : NSObject <NSURLConnectionDelegate>

@property (nonatomic, strong) WCFServiceRequest *request;

@end

Step 5: Make the Web Service Call

Implement the connection:didReceiveAuthenticationChallenge: method to handle the authentication challenge:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    // Get the certificate from the request object
    NSData *certificate = self.request.certificate;
    
    // Create an NSX509Certificate object from the certificate data
    NSX509Certificate *x509Certificate = [[NSX509Certificate alloc] initWithData:certificate];
    
    // Respond to the authentication challenge with the X.509 certificate
    [challenge respondWithAuthenticationChallenge:[NSURLAuthenticationChallenge authenticationChallengeUsingProtectionSpace:challenge.protectionSpace authenticationData:x509Certificate]];
}

Step 6: Assemble and Launch the App

Assemble and launch your iPhone app. The app should now make a web service call to the WCF service using certificate authentication.

Conclusion

In this article, we explored how to make iPhone web service calls to a WCF service using certificate authentication. We covered the necessary steps to set up the WCF service, generate a certificate, and create an iOS app that makes the web service call using NSURLConnection.

Remember to handle the authentication challenge correctly by responding with the X.509 certificate. This will ensure successful communication between your iPhone app and the WCF service.

Additional Notes

  • Make sure to test your WCF service on a local machine or a remote server before deploying it on an actual device.
  • When deploying the certificate to your iPhone app, use the NSX509Certificate class to convert the certificate data into an X.509 format compatible with iOS.
  • If you encounter any issues during development, refer to the Apple documentation for NSURLConnectionDelegate and ensure that your WCF service is properly configured with Transport security using certificates.

By following these steps and understanding the underlying concepts, you’ll be able to successfully integrate your iPhone app with a WCF service using certificate authentication.


Last modified on 2023-11-08