Exercise 2: Creating the Windows Phone 7 Application

In this exercise, you will create a Windows Phone 7 application to query the maintenance tasks SharePoint list. You will also implement the code necessary to lock and unlock the application.

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.Private.PartsClient.sln file located at %TrainingKitPath%\Labs\CreatingPrivateApplications\Source\Before\WP7.Private.PartsClient 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.Private.PartsClient project, in the Utilities folder, open the Constants.cs file.
  2. Change the value for the USER_NAME and USER_PASSWORD constants to represent a Forms Based Authentication user specific to your development environment. For this lab, the user requires reader permissions.
  3. Change the value for the AUTHENTICATION_SERVICE_URL constant to the URL specific to your development environment.

    The following code example demonstrates the value for a SharePoint server named fbawp7.

    C#

    public const string AUTHENTICATION_SERVICE_URL = "http://fbawp7/_vti_bin/authentication.asmx";

Task 3 – Configuring the Reference to the SharePoint Lists.asmx Web Service

In this task, you will configure the reference to the SharePoint lists.asmx Web service.

  1. In the Solution Explorer, double click the ServiceReferences.ClientConfig file to open it.
  2. In the Endpoint element, change the address attribute to the URL for the lists.asmx SharePoint web service in the site where you created the Maintenance Tasks list.

    Example: http://fbawp7/_vti_bin/lists.asmx

    Figure 4

    Configure the Lists service endpoint address

Task 4 – Implementing Code to Lock and Unlock the Application

In this task, you will use the cryptographic APIs to lock and unlock the windows phone application. The cryptographic APIs provide a secure mechanism to ensure the data used to unlock the phone application is valid.

  1. In the WP7.Private.PartsClient project, expand the Register.xaml file, and double click the Register.xaml.cs file.
  2. Add the following code under the //TODO: 9.1.1 comment to define the buttonRegister_Click event handler:

    C#

    private void buttonRegister_Click(object sender, RoutedEventArgs e) { try { string encryptedAppKey = Utils.Register(txtApplicationID.Text, txtApplicationPassword.Text, txtApplicationRegistrationKey.Text); if (encryptedAppKey == Constants.ENCRYPTED_APP_KEY) { NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); } else { MessageBox.Show("The application could not be registered. Please verify " + "the Application ID, Application Password, and Registration Key are " + "correct and try again.", "Registration Error", MessageBoxButton.OK); } } catch (System.Security.Cryptography.CryptographicException cryptographicException) { MessageBox.Show(cryptographicException.Message, "Encryption Error", MessageBoxButton.OK); } catch (System.Exception exception) { MessageBox.Show(exception.Message, "Error", MessageBoxButton.OK); } }

    The buttonRegister_Click event handler is called when a user clicks the Register button in the Windows Phone 7 application. The above code calls the Register method to validate the input from the user and return an encrypted string. Then, the event handler compares the encrypted string returned from the Register method and the encrypted application key defined in the Constants class. If the values match, the user entered the correct information to unlock the application and the application opens the MainPage user control. If the values do not match, the user entered incorrect information to unlock the application and the application prompts the user to validate their input and try again.

  3. In the WP7.Private.PartsClient project, expand the Utilities folder and double click the Utils.cs file.
  4. Add the following code under the //TODO: 9.1.2 comment to define the Register method:

    C#

    public static string Register(string applicationID, string applicationPassword, string applicationRegistrationKey) { AesManaged aesManaged = null; MemoryStream memoryStream = null; CryptoStream cryptoStream = null; try { Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(applicationPassword, Encoding.UTF8.GetBytes(applicationRegistrationKey), 99999); aesManaged = new AesManaged(); aesManaged.IV = rfc2898DeriveBytes.GetBytes(16); aesManaged.Key = rfc2898DeriveBytes.GetBytes(32); memoryStream = new MemoryStream(); cryptoStream = new CryptoStream(memoryStream, aesManaged.CreateEncryptor(), CryptoStreamMode.Write); byte[] bytes = Encoding.UTF8.GetBytes(applicationID); cryptoStream.Write(bytes, 0, bytes.Length); cryptoStream.FlushFinalBlock(); return Convert.ToBase64String(memoryStream.ToArray()); } finally { if (aesManaged != null) { aesManaged.Clear(); } if (memoryStream != null) { memoryStream.Close(); } if (cryptoStream != null) { cryptoStream.Close(); } } }

    The Register method uses the cryptographic APIs to create an encrypted string. The cryptographic APIs use the applicationID, applicationPassword, and applicationRegistrationKey arguments to create the encrypted string. In this scenario, the Advanced Encryption Standard (AES) algorithm encrypts the string. AES uses a symmetric-key algorithm to encrypt data.

    The following table describes how the arguments in the Register method are used by the cryptographic APIs use to create the encrypted string.

    Register method parameter

    Cryptographic API

    applicationID

    The string to encrypt.

    applicationPassword

    The password.

    applicationRegistrationKey

    The salt.

  5. Save Utils.cs.

Task 5 – Testing the Application

In this task, you will test the Windows Phone 7 application.

  1. In the WP7.Private.PartsClient solution, select Windows Phone 7 Emulator in the deployment location dropdown list.
  2. Press F5.
  3. The Windows Phone 7 application starts in the emulator prompts for the information necessary to unlock the application. The correct values are pre-filled for the lab. In a real world scenario, the user would be required to enter values necessary to unlock the phone application.

    Figure 5

    The application registration screen

  4. Click in the Application Password textbox and enter a character.

    Figure 6

    Invalid registration entry

  5. Click the Register button.

    Figure 7

    Registration failure exception

  6. The Windows Phone 7 application displays an error message that the application registration failed. It does not allow you to use the rest of the application.
  7. Click OK.

    Figure 8

    Fix the registration code

  8. Click in the Application Password textbox and delete the character you entered.
  9. Click Register.
  10. The Windows Phone 7 application displays the items from the Maintenance Requests SharePoint list.

    Figure 9

    Application registered successfully

  11. Click on one of the maintenance requests in the list to select it.
  12. The Windows Phone 7 application displays the details for the selected maintenance request from the SharePoint list.

    Figure 10

    Application activation complete