Exercise 2: Creating the Windows Phone 7 Application

In this exercise, you will complete a Windows Phone 7 application that can read from the maintenance announcements SharePoint list created in exercise 1. This exercise focuses on the steps to authenticate to SharePoint using a UAG server.

Task 1 – Beginning the Exercise

In this task, you will open the lab solution in Visual Studio 2010.

  1. Make sure that you have downloaded and installed the items listed in System Requirements above prior to beginning this exercise.
  2. Launch Visual Studio 2010 as administrator and open the lab project by selecting File » Open » Project.
    1. Browse to the WP7.Security.UAG.sln file located at %TrainingKitPath%\Labs\IntegratingUnifiedAccessGatewayUAG\Source\Before and select it.
    2. Click Open to open the solution.

Task 2 – Configuring Constants in the Windows Phone 7 Application

In this task, you will configure the constants used in the Windows Phone 7 application to work with your development environment.

  1. In the WP7.Security.UAG, in the Utilities folder, open the Constants.cs file.
  2. Change the value for the USER_NAME and USER_PASSWORD constants to represent a user specific to your development environment. For this lab, the user requires read permissions.
  3. Change the value for the LISTS_SERVICE_URL constant to the URL specific to your development environment.
  4. The following code example demonstrates the value for a SharePoint server named spwp7intranet.contoso.com.

    C#

    public const string LISTS_SERVICE_URL = "https://spwp7intranet.contoso.com/_vti_bin/lists.asmx";

Task 3 – Adding the User-Agent Header to the Web Request

In this task, you will add the User-Agent Header to the Web request. The User-Agent Header is required to authenticate to the SharePoint server via the UAG Server.

  1. In the WP7.Security.UAG, in the ViewModels folder, open the MainViewModel.cs file.
  2. Add the following code under the //TODO: 6.2.1 comment to add the User-Agent Header to the Web request:

    C#

    spListsService.Headers["User-Agent"] = "Microsoft Office Mobile";

    When a request to the SharePoint server is processed by the UAG server, the ‘Microsoft Office Mobile’ User-Agent Header value causes the UAG server to use the credentials in the Authorization Header to attempt to authenticate the user instead of issuing a 401 to prompt the user for credentials. Windows Phone 7 applications are unable to respond to 401s issued by web sites, therefore setting the User Agent Header value is critical to allow the credentials to pass through.

Task 4 – Adding the Authorization Header to the Web Request

In this task, you will add the Authorization Header to the Web request. The Authorization Header is required to authenticate to the SharePoint server via the UAG Server.

  1. In the WP7.Security.UAG, in the ViewModels folder, open the MainViewModel.cs file.
  2. Add the following code under the //TODO: 6.2.2 comment add the Authorization Header to the Web request:

    C#

    spListsService.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(Constants.USER_NAME + ":" + Constants.USER_PASSWORD)) + System.Environment.NewLine;

    The UAG Server uses the values in the Authorization Header to attempt to authenticate the user. Both the user’s account name and password are encoded and added to the Authorization header. The UAG Server attempts to authenticate the user based on the credentials provided. If the credentials are valid, the UAG server redirects the request to the requested resource on the SharePoint server. In this case, the requested resource is the lists.asmx SharePoint Web Service.