Share via


Windows Azure Web Role WCF Service Federated Authentication Using AppFabric Access Control Service (ACS) v2 – Part 1

Programming Windows Azure - Programming the Microsoft Cloud

This is continuation to the series on how to build distributed application hosted in Windows Azure Environment and Security using Windows Azure AppFabric Access Control Service (ACS) v2. For previous parts review the following:

This is first part of the series on how to secure Windows Azure deployed WCF service using ACS v2.0. It assumes you have completed previous parts outlined above.

Step 1 – Create Windows Azure Web Role WCF Service project in Visual Studio 2010

Content in this step is adopted, adapted, and extended based on Code Quick Start: Create and deploy a WCF service in Windows Azure and Code Quick Start: Create a client application that uses a WCF service deployed to Windows Azure

To create and run a WCF application for Windows Azure
  1. Switch back to Visual Studio 2010 make sure it runs with administrator privileges. To launch Visual Studio with administrator privileges, right-click Microsoft Visual Studio 2010 and then click Run as administrator.

  2. Right click on your solution in the Solution Explorer and choose Add and then New Project... option.

  3. Within the New Project dialog, navigate to Installed Templates, Visual C# , and click Cloud.

  4. Click Windows Azure Project. If needed, modify the Name: and the Location: fields, which indicates where your solution will be stored. Click OK to close the New Project dialog.

  5. Within the New Windows Azure Project dialog, navigate to Visual C# , click the WCF Service Web Role, and then click the > symbol. This will add a web role to your Windows Azure solution. A web role provides an environment for running web sites or applications as supported by Internet Information Services (IIS) 7.0. Click OK to close the New Windows Azure Project dialog.

  6. Modify IService1.cs to contain one operation, ReverseString. To navigate to IService1.cs, open Solution Explorer. If Solution Explorer is not visible, from the View menu click Solution Explorer. Within Solution Explorer, expand WCFServiceWebRole1 and double-click IService1.cs. Modify the contents of IService1.cs to be the following code:

     {
    
        [ServiceContract]
        public interface IService1
        {
    
            [OperationContract]
            string ReverseString(string stringToReverse);
        }
    
    }
    

    Save and close IService1.cs.

  7. Modify Service1.svc.cs to contain the implementation of the operation GetHello. (Use Solution Explorer to navigate to Service1.svc.cs similar to the way you navigated in the previous step.) Modify the contents of Service1.svc.cs to be the following code:

     {
    
        public class Service1 : IService1
        {
            public string ReverseString(string stringToReverse)
            {
                 char[] chars = stringToReverse.ToCharArray();
                 Array.Reverse(chars);
                 return new string(chars);
    
             }
        }
    }
    
  8. Save and close Service1.svc.cs.

  9. Set the WCF project as a start up project by right clicking on it in Solution Explorer and choosing Set as StartUp Project option.

  10. Set Service1.svc as a start page by right clicking on it and choosing Set As StartUp option.

  11. Compile and run the service by clicking Debug from the menu and then clicking Start Without Debugging, or using Ctrl+F5.

  12. You should see WCF Test Client start up.

  13. Double click ReverseString() node on the left in the WCF Test Client. You should see Request and Response sections on the right.

  14. In the Request section type Hello World! in the Value field in the row where stringToReverse appears under the Name field.

  15. Click on Invoke button located between the Request and Response sections.

  16. You should see "!dlroW olleH" shows in the Value field in the Response section.

  17. Add a web service reference to your WCF service in ASP.NET web application. To add a web service reference, open Solution Explorer. If Solution Explorer is not visible, from the View menu click Solution Explorer. Within Solution Explorer, expand the nodes until you see References under your ASP.NET web application. Right-click References and click. Add service reference… . Within the Add Service Reference dialog, click on Discover button. It should list Service1 in the Services: section. Click on Advanced button. Ensure Always generate message contracts is checked in the Service Reference Settings dialog. Click OK to close the Service Reference Settings dialog. Click OK to close the Add Service Reference dialog.

  18. Open Default.aspx.cs. To open Default.aspx.cs, navigate to Default.aspx in Solution Explorer and expand it. You should see now Default.aspx.cs, double click it. Modify the contents Page_Load to be the following code:

    protected void Page_Load(object sender, EventArgs e)

    {

    ServiceReference1.Service1Client client = null;

    try

    {

    client = new ServiceReference1.Service1Client();

    ServiceReference1.ReverseStringRequest request = new ServiceReference1.ReverseStringRequest("Hellow World!");

    ServiceReference1.ReverseStringResponse response;

    response = client.ReverseString(request);

    Response.Write(HttpUtility.HtmlEncode("The WCF service called returned: '" + response.ReverseStringResult + "'"));

    }

    catch (Exception ex)

    {

    Response.Write(HttpUtility.HtmlEncode("Exception encounter: '" + ex.Message + "'"));

    }

    finally

    {

    if (null != client)

    {

    client.Close();

    }

    }

    }

  19. Save and close Default.asp.cs.

  20. Set your ASP.NET web application as a start up project by right clicking on it in Solution Explorer and choosing Set As StartUp Project. The one that has Roles folder in it. It does not have actual web content in it.

  21. Run the WCF service first by right clicking on it in Solution Explorer and choosing Debug and then Start new instance option. You should see WCF Test client appears. Do not close it.

  22. Now run the ASP.NET application by right clicking on it and choosing Debug and then Start new instance option.

  23. You should be presented with the page that offers you to choose Google or Windows Live ID as an options.

  24. After successful authentication with either of these you should be presented with the Default.aspx page with the following line at the top: The WCF service called returned: '!dlroW wolleH'

  25. CAVEATS: you will need to make couple of changes in both web.config and ACS v.2 management portal to make sure ACS v2.0 redirects you back to your development environment and not to the one already deployed to Windows Azure.