Currently I am working with some colleagues to build a demo application that consists out of a back office application and a mobile application that runs
on IPhone, IPad, Android and Windows Phone 7. We decided to build the simulation of the back office using RIA services since that gives the fastest results
if it comes to building CRUD like services and exposing them in a easy and authenticated way. Since all Phone platforms can be targeted using C# with the
Novel Mono development environments, we thought it would be nice to show how we can use the RIA services exposed as a SOAP endpoint.

since we want to simulate a real life scenario where you always need to authenticate the end-user before you serve him with some data, we also wanted to
enable authentication on the services.

So we build an easy data model using the Entity Framework that represents the notion of a Asset Tracking system, where users can register assets and
share them amongst their peers. when you wan to know where your assets are, you can access them on your mobile device and query there whereabouts.
Also when you acquire an asset, you can register it at the service and provide location information using longitude and latitude using a Map control and
leveraging phone location awareness.

The data model looks like follows:

EntityDesignerDiagram

I Created two simple domain service classes. One to manage the authentication using the ASP.NET membership API and on that contains the three entities.
To manage authentication I provided a database with the ASP.NET schema in it and configured it in the web.config file.

My system.web part of the configuration file looks as follows to enable the authenticated service calls:

 

And I added the SOAP endpoint, so I could query my domain service for it’s service contract.

 

In order to be able to filter the requested entities for a specific user, I added the UserName property to each entity. While not an ideal solution, this suffices for this demo.

Next is to make sure the service operations are only returning data for the current authenticated user. this is shown in the below code sample:

So now I have my domain service I can create my Phone application, and I will show you how you can call the RIA services in an authenticated way.

First you add a service reference to the domain service that provides you the entities you are intereseted in. In this example that results in a service reference

that generates a proxy that contains all the operations I can use at the client. Because I Use Ria Services, all service calls follow the async pattern. So my

GetAssets operation is available in the generated proxy as GetAssetsAsync() and you set up an event handler for the GetAssetsCompleted event.

This looks like follows in code:

 

The only problem with this code is that it will generate an exception, since I did not authenticate myself to the service.

to solve this problem, you also need a service reference to the AuthenticationDomain service. So next you generate a proxy for that SOAP endpoint as well

and that will result in a generated proxy you can call to login. Now when you search on this topic, you will find all kinds of posts telling that RIA services is not

supported on the phone, but the fact is that it is nothing more then a WCF service and we know it uses a cookie to authenticate each next request after you log in.

So with that knowledge you can configure the binding you need with just WCF configuration. Since we use the ASP.NET forms authentication protocol behind the scenes,

the only thing you need to do is enable the CookieContainers on the bindings of the services that got generated. This looks as follows for your phone client.config file

Now that we have enabled the authentication protocol we need to first call the AuthenticationDomain proxy and authenticate the user. Next wait for the asynchronous response and then when the logon was successful add the cookies from the proxy that got returned to the proxy that is going to make then next call.

This looks as follows in code:

And this all results in the final proof of concept that I can use authenticated RIA services on my Window Phone 7 Device. Result is shown below:

image

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Hope this helps,

cheers,

Marcel

CTO at Xpirit, Microsoft Regional Director, Visual studio ALM MVP, Speaker, Pluralsight Author and IT Architect Consultant