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
- Run Visual Studio.
- In the menu choose File, New, Project… .
- In the New Project dialog box click on Windows node on the left, select WPF Application from the list, and click OK button.
- MainWindow.xaml should open in the designer. If it is not, double click on it in the Solution Explorer.
- From the Toolbox drag Button and Listbox controls on the Grid. .
- 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
In the Solution Explorer right click on the References node and select Add References…
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.
If MainWindow.xaml.cs us bit already opened in the editor open it by double clicking on it in the Solution Explorer.
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;Save your work.
Step 3 – Implement The Code That Retrieves IdP’s
To implement the code that retrieves IdP’s
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.
Add the following declaration to the top of the IdpInfo.cs file:
using System.Runtime.Serialization;
Paste the following code for the IdpInfo class:
[DataContract]
public class IdentityProviderInfo
{
[DataMember]
public string Name { get; set; }[DataMember]
public string LoginUrl { get; set; }}
Save your work.
Open MainWindow.xaml.cs file in the editor by double clicking on it in the Solution Explorer.
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";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>;
}
}Call GetIdentityProviders() method in the button1_Click event handler.
Open MainWindow.xaml markup in the editor by double clicking on it in the Solution Explorer.
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>Compile the solution to make sure there is no compilation errors.
Step 4 – Test Your Solution
To test your solution
- Press F5 to run the solution. You should see MainWindow application appears with the button and the empty listbox.
- 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.
- 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):