Windows Phone 7 and RESTful Services: Delegated Access Using Azure AppFabric Access Control Service (ACS) And OAuth
This post is a summary of steps I have taken in order to run the Windows Phone 7 Sample that Caleb Baker recently published on ACS Codeplex site. The sample demonstrates how to use Azure AppFabric Access Control Service to allow end users sign in to RESTful service with their Windows Live ID, Yahoo!, Facebook, Google, or corporate accounts managed in Active Directory.
This sample answers the following question:
How can I externalize authentication when accessing RESTful services from Windows Phone 7 applications?
Scenario:
Solution:
Summary of steps:
- Step 1 – Prepare development environment
- Step 2 – Create and configure ACS project, namespace, and Relying Party
- Step 3 – Create Windows Phone 7 Silverlight application (the sample code)
- Step 4 – Enhance Windows Phone 7 Silverlight application with OAuth capability
- Step 5 – Test your work
Step 1 – Prepare development environment
There are several prerequisites needed to be installed on the development environment. Here is the list:
- Visual Studio 2010 installed.
- IIS7 is installed and running.
- Download and extract the Windows Phone 7 Sample code.
- Download and install Windows Phone Developer Tools RTW.
- Download and extract DPE.OAuth project from the FabrikamShipping Demo, available as part of the Source Code.
Step 2 – Create and configure ACS project, namespace, and Relying Party
- Login to https://portal.appfabriclabs.com/
- If you have not created your project and a namespace, do so now.
- On the Project page click on “Access Control” link next to your namespace in the bottom right corner.
- On the “Access Control Settings” page click on “Manage Access Control” link at the bottom.
- Sign in using any of the available accounts. I used Windows Live ID.
- On the “Access Control Service” page click on “Relying Party Applications” link.
- On the “Relying Party Applications” page click on “Add Relying Party Application”.
- Fill the fields as follows:
- Name: ContosoContacts
- Realm: https://ContosoContacts/
- Return URL: https://localhost:9000/Default.aspx
- Token format: SWT
- Token lifetime (secs): leave default, 600
- Identity providers: leave default, I have Google and Windows Live ID
- Rule groups: leave default “Create New Rule Group” checked
- Click “Generate” button for the token signing key field. Copy the key into a safe place for further reuse.
- Click “Save” button.
- Click on “Access Control Service” link in the breadcrumb at the top.
- On the “Access Control Service” page click on the “Rule Groups” link.
- On the “Rule Groups” page click on the “Default Rule Group for ContosoContacts” link.
- On the “Edit Rule Group’ page click on “Generate Rules” link at the bottom.
- On the “Generate Rules: Default Rule Group for ContosoContacts” page click on “Generate” button.
- On the “Edit Rule Group” page click “Save” button.
- Click on the “Access Control Service” link in the breadcrumb at the top.
- On the “Access Control Service” page click on “Application Integration” at the bottom.
- On the “Application Integration” page click on “Login Pages” link.
- On the “Login Page Integration” page click on ContosoContacts relying party link.
- On the “Login Page Integration: ContosoContacts” page copy the link at the bottom without the ending “&callback=”. It will be used in the application itself. Paste it to a safe place for further reuse. Mine looks as follows:
Step 3 – Create Windows Phone 7 Silverlight application (the sample code)
- Make sure you completed all requirements in the Step 1 – Prepare your development environment.
- Run Visual Studio 2010 as Administrator.
- Open ContosoContactsApp.sln – this is the code you downloaded and extracted as part of the Windows Phone 7 Sample code.
- Open SignIn.xaml.cs in ContosoContactsApp project.
- Locate SignInControl.GetSecurityToken( new Uri(“...”)); and update with the URI from the previous step.
- Open MainPage.xaml.cs in teh same project, ContosoContactsApp.
- Locate client.DownloadStringAsync(new Uri("...”)); and update with https://localhost/contacts/Directory.
- Add CustomerInformationService project to the solution. You will be prompted to create IIS Virtual Directory for it.
Step 4 – Enhance Windows Phone 7 Silverlight application with OAuth capability
- Add DPE.OAuth project to the solution. The project comes as part of the Source Code for FabrikamShipping Demo.
- Add a reference to the DPE.OAuth project from the CustomerInformationService project.
- Open web.config of the CustomerInformationService project.
- Locate issuerIdentifier=”https://[Service Namespace].accesscontrol.appfabriclabs.com/” entry.
- Replace [Service Namespace] with your namespace. In my case it is my-namespace1. Look at (21) for reference in Step 2 – Create and configure ACS project, namespace, and Relying Party.
- Locate serviceKey="[Insert Symmetric Token Signing Key]" entry and update with your Key. Look at (9) for reference in Step 2 – Create and configure ACS project, namespace, and Relying Party. If you missed that step. Navigate to the ACS portal at https://portal.appfabriclabs.com/, click on your project, click on the “Access Control” link next to your namespace, click on “Manage Access Control” link, click on “Certificates and Keys”, click on ”Default Signing Key for ContosoContacts”, copy the value in the Key filed on the “Edit Token Signing Certificate or Key” page.
Step 5 – Test your work
- Switch to Visual Studio.
- Make sure ContosoContactsApp solution selected.
- Press F5 button.
- You should see the Sign In screen:
- Click on Sign In link. You should be presented with the options for Signing In:
- I clicked on Google which resulted in presenting me with Google’s sign page:
- After I provided my credentials, Google asked me for my consent that a service is asking for information on my behalf:
- Click on “Allow” button and receive desired information from the service:
Please provide feedback how to improve in the comments section below.
Related Books
- Programming Windows Identity Foundation (Dev - Pro)
- A Guide to Claims-Based Identity and Access Control (Patterns & Practices) – free online version
- Developing More-Secure Microsoft ASP.NET 2.0 Applications (Pro Developer)
- Ultra-Fast ASP.NET: Build Ultra-Fast and Ultra-Scalable web sites using ASP.NET and SQL Server
- Advanced .NET Debugging
- Debugging Microsoft .NET 2.0 Applications
Related Info
- Windows Identity Foundation (WIF) and Azure AppFabric Access Control (ACS) Service Survival Guide
- Windows Identity Foundation (WIF) Fast Track
- Windows Identity Foundation (WIF) Code Samples
- Windows Identity Foundation (WIF) SDK Help Overhaul
- Windows Identity Foundation (WIF) and Azure AppFabric Access Control (ACS) Service Survival Guide
Comments
- Anonymous
November 12, 2010
Very nice.I think authentication is a key scenario, especially oAuth for any phone/cloud/rest scenarios.