Samstag, 13. November 2010

Using authenticated RIA Services with Windows Phone 7

In my older post RIA Services: Windows Phone 7 and SOAP endpoint | .NET - Red zone : Best practices and latest stuff I have described how a RIA Service can be used as normal WCF SOAP service for example with Windows Phone 7.

But if you want to use RIA Services with Windows Phone 7, there is the problem that Silverlight for Windows Phone 7 does not support authentication headers. But using the instructions here Using Authenticated Ria Services on your WP7 phone - Marcel de Vries, MVP Team System - blog community you can still use the cool authorization mechanism of WCF RIA Services with Windows Phone 7.

The key steps are:

1. Add enableHttpCookieContainer="true" to your ServiceReferences.ClientConfig

After the proxy is generated enable the HttpCookieContainer at your bindings for authentication service and your domain service. You have to remove it for each proxy new configuration to avoid an error message during proxy generation.

   1: <bindings>
   2:             <basicHttpBinding>
   3:                 <binding name="BasicHttpBinding_AuthenticationServiceSoap" maxBufferSize="2147483647"
   4:                          enableHttpCookieContainer="true"
   5:                     maxReceivedMessageSize="2147483647">
   6:                     <security mode="None" />
   7:                 </binding>
   8:                 <binding name="BasicHttpBinding_EmployeeDomainServiceSoap" maxBufferSize="2147483647"
   9:                       enableHttpCookieContainer="true"
  10:                     maxReceivedMessageSize="2147483647">
  11:                     <security mode="None" />
  12:                 </binding>
  13:             </basicHttpBinding>
  14:         </bindings>
2. Store the returned cookie container of your Login-Call for the next service calls
   1: protected virtual void OnLoginCompleted(object sender, LoginCompletedEventArgs e)
   2:        {
   3:            var args = new LoginServiceCompletedEventArgs();
   4:            
   5:            CookieContainer = null;
   6:            
   7:            if (e.Result != null)
   8:            {
   9:                args.User = null;
  10:                args.Error = true;
  11:  
  12:                CookieContainer = _authclient.CookieContainer;
  13:                ViewModelLocator.AuthCookieContainer = _authclient.CookieContainer;

Hint: Don’t be surprised that the cookie container looks empty at this point (count is 0). There is a kind of “magic” cookie still in there.

3. Use the returned cookie container for your proteced domain service calls
   1: var client = new EmployeeDomainServiceSoapClient
   2:                  {
   3:                      CookieContainer = ViewModelLocator.AuthCookieContainer
   4:                  };

1 Kommentar: