The challenge:
Create a WCF Service with Basic Authentication.
Consume the service with Adobe ActionScript® 3 (FLEX)
The outcome:
VICTORY!!!!
How I did it:


WCF code:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together. public class Service : IService { public string GetData(int value) { return string.Format("You entered: {0}", value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite == null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } }
WCF web.config
<configuration> ... <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpEndpointBinding"> <security mode="TransportCredentialOnly"> <transport clientCredentialType="Basic" /> </security> </binding> </basicHttpBinding> </bindings> <services> <service behaviorConfiguration="ServiceBehavior" name="Service"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding" name="B" contract="IService"> <identity> <dns value="localhost" /> </identity> </endpoint> <!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />--> </service> </services> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <!-- To avoid disclosing metadata information, set the value below to false before deployment --> <serviceMetadata httpGetEnabled="true" /> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
Adobe ActionScript® 3 code
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()"> <mx:Label id="myLabel"/> <mx:Script> <![CDATA[ import flash.events.*; import flash.net.*; import mx.utils.*; public function init():void { URLRequestExample(); } private function URLRequestExample():void { // Encode username and password var encoder:Base64Encoder = new Base64Encoder(); encoder.encode("Administrator:xxxx"); // Initialize headers var authorizationHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + encoder.toString()); var soapActionHeader:URLRequestHeader = new URLRequestHeader("SOAPAction","http://tempuri.org/IService/GetData"); // Initialze request var request:URLRequest = new URLRequest("http://localhost:8000/WCFServiceBasicHttp/Service.svc"); request.contentType = "text/xml;charset=UTF-8"; request.method = URLRequestMethod.POST; request.requestHeaders.push(authorizationHeader); request.requestHeaders.push(soapActionHeader); // This is the actual soap envolope that will be send to the WCF service, generated with SOAPUI request.data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"><soapenv:Header/><soapenv:Body><tem:GetData><tem:value>3</tem:value></tem:GetData></soapenv:Body></soapenv:Envelope>'; // Initalize the URLLoader var loader:URLLoader = new URLLoader(); configureListeners(loader); try { // Send out the service call loader.load(request); trace("Request sent"); } catch (error:Error) { trace("Unable to load requested document."); } } private function configureListeners(dispatcher:IEventDispatcher):void { dispatcher.addEventListener(Event.COMPLETE, completeHandler); dispatcher.addEventListener(Event.OPEN, openHandler); dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler); dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler); dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); } private function completeHandler(event:Event):void { var loader:URLLoader = URLLoader(event.target); trace("completeHandler: " + loader.data); // Output response to myLabel myLabel.text = loader.data; } private function openHandler(event:Event):void { trace("openHandler: " + event); } private function progressHandler(event:ProgressEvent):void { trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal); } private function securityErrorHandler(event:SecurityErrorEvent):void { trace("securityErrorHandler: " + event); } private function httpStatusHandler(event:HTTPStatusEvent):void { trace("httpStatusHandler: " + event); } private function ioErrorHandler(event:IOErrorEvent):void { trace("ioErrorHandler: " + event); } ]]> </mx:Script> </mx:Application>

