Share via


How To: Display List Of Identity Providers (IdP’s) For Windows Azure AppFabric ACS Namespace In WPF Application

This is a quick walkthrough of the code required to obtain the list of Identity Providers (IdP’s) configured for specific ACS namespace. The code in this post is vastly simplified and based on what’s demonstrated in the Code Sample: Windows Phone 7 Application. To obtain the list of IdP’s for any ACS namespace authentication is not required – this information is available without authentication.

Summary of steps

  • Step 1 – Create Basic WPF Application
  • Step 2 – Add Required Assemblies And Namespaces
  • Step 3 – Implement The Code That Retrieves IdP’s
  • Step 4 – Test Your Solution

Step 1 – Create Basic WPF Application

To create basic WPF application

  1. Run Visual Studio.
  2. In the menu choose File, New, Project… .
  3. In the New Project dialog box click on Windows node on the left, select WPF Application from the list, and click OK button.
  4. MainWindow.xaml should open in the designer. If it is not, double click on it in the Solution Explorer.
  5. From the Toolbox drag Button and Listbox controls on the Grid. .
  6. Double click on the Button. button1_Click event handler should open for editing in the MainWindow.xaml.cs file. This is where you will be implementing the code that retrieves the list of available IdP’s for specific ACS namespace.

Step 2 – Add Required Assemblies And Namespaces

To add required assemblies and namespaces

  1. In the Solution Explorer right click on the References node and select Add References…

  2. Click on the .NET tab and select System.Net, System.Web, System.ServiceModel.Web, System.Runtime.Serialization assemblies from the list. Press Ctrl to make multiple selection.

  3. If MainWindow.xaml.cs us bit already opened in the editor open it by double clicking on it in the Solution Explorer.

  4. Add the following declarations to the MainWindows.xaml.csfile:

    using System.Globalization;
    using System.Web;
    using System.Net;
    using System.IO;
    using System.Runtime.Serialization.Json;
    using System.Collections.ObjectModel;

  5. Save your work.

Step 3 – Implement The Code That Retrieves IdP’s

To implement the code that retrieves IdP’s

  1. Add new class to the solution by right clicking on it in the Solution Explorer and choosing Add, Class… option. Give a name to the new class, for example, IdpInfo.cs.

  2. Add the following declaration to the top of the IdpInfo.cs file:

    using System.Runtime.Serialization;

  3. Paste the following code for the IdpInfo class:

    [DataContract]
    public class IdentityProviderInfo
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string LoginUrl { get; set; }

    }

  4. Save your work.

  5. Open MainWindow.xaml.cs file in the editor by double clicking on it in the Solution Explorer.

  6. Add the following private members to the MainWindow class, these should be exactly copy and pasted from your configuration on ACS management portal, Edit Relying Party Application page.

    private string m_realm = "https://YourRealm/";
    private string m_serviceNamespace = "YourNamespace";
    private string m_acsHostUrl = "accesscontrol.windows.net";

  7. Add the following two private methods to the MainWindowclass:

    private void GetIdentityProviders()
    {
        {
            Uri identityProviderDiscovery = new Uri(
                string.Format(CultureInfo.InvariantCulture,
                    "https://{0}.{1}/v2/metadata/IdentityProviders.js?protocol=javascriptnotify&realm={2}&version=1.0",
                    m_serviceNamespace,
                    m_acsHostUrl,
                    HttpUtility.UrlEncode(m_realm)),
                    UriKind.Absolute
                );

            WebClient webClient = new WebClient();

            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            webClient.DownloadStringAsync(identityProviderDiscovery);
        }
    }
    private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(e.Result)))
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IdpInfo[]));
            listBox1.ItemsSource = serializer.ReadObject(ms) as IEnumerable<IdpInfo>;
        }
    }

  8. Call GetIdentityProviders()   method in the button1_Click event handler.

  9. Open MainWindow.xaml markup in the editor by double clicking on it in the Solution Explorer.

  10. Add the following item template to the Listbox to make sure the IdP’s Name property is bound to be displayed in the Listbox.

    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>

  11. Compile the solution to make sure there is no compilation errors.

Step 4 – Test Your Solution

To test your solution

  1. Press F5 to run the solution. You should see MainWindow application appears with the button and the empty listbox.
  2. Click on the button once, the list box should be filled with the names of the IdP’s for the specific ACS namespace and the real you specified.
  3. This is how mine looks for the following app - https://wawithacsv2.cloudapp.net/ (need to figure out what are these special characters in Live ID IdP):

image