Alik Levin's

Clarity, Technology, and Solving Problems | PracticeThis.com 

September, 2011

  • Alik Levin's

    How To: Obtain SWT Security Token From Windows Azure AppFabric ACS In WPF Application Using WebBrowser Control

    • 0 Comments

    Programming Windows Identity Foundation

    This post assumes you have completed steps outlined in Displaying List Of Identity Providers (IdP’s) For Windows Azure AppFabric ACS Namespace In WPF Application

    In this post I will show how to obtain SWT token from Windows Azure AppFabric Access Control Service (ACS) in WPF application using WebBrowser control when federating with Internet Identity Providers (IdP’s) such as Live ID, Google, Facebook, Yahoo!, or Open ID identity providers. The SWT token can then be used when communicating with RESTful WCF service that requires issued SWT token for authentication purposes.

    If you are interested to obtain SWT token from ACS when ACS manages service credentials (vs. Internet IdP’s) using Service Identities refer to the following article - WCF (REST) Service With Federated Authentication.

    Summary of steps

    • Step 1 – Add WebBrowser And Other Controls To Your WPF Application
    • Step 2 – Configure The WebBrowser Control For Interaction Between Its Contents And The WPF Application
    • Step 3 – Test Your Solution

    Step 1 – Add WebBrowser And Other Controls To Your WPF Application

    To add controls to your WPF application

    1. From the toolbox drag TextBox control and place it beneath ListBox control on the Grid. This is not required and it will be used for visualization purposes only. This is where you will see the selected IdP’s sign in URL.
    2. Double click on the ListBox control. It should open listBox1_SelectionChanged event handler in MainWindow.xaml.cs file for editing.
    3. Paste the following code as the event handler implementation. Clicking on different IdP’s in the ListBox will show sign in URL related to the selected IdP.

      string idpUrl = ((IdpInfo)e.AddedItems[0]).LoginUrl;
      textBox1.Text = idpUrl;

    4. From the toolbox drag WebBrowser control on the Grid next to the ListBox. This is where the IdP’s sign in web form will display.
    5. From the toolbox drag another Button control on the Grid and place it above the WebBrowser control. Clicking on the button will trigger the WebBrowser control navigate to the sign in URL of the selected IdP. Double click on the button to open button2_Click event handler in the MainWindow.xaml.cs file for editing.
    6. Paste the following code as the button2_Click event handler’s implementation. Remember I am using textBox1for visualization purposes only and it is not required. 

      webBrowser1.Navigate(textBox1.Text);

    7. From the toolbox drag another TextBox control on the grid and place it beneath all other controls. Resize it to accommodate large amount of text. It will display acquired SWT token. This is not required and it is used for visualization purposes only. Configure its TextWrapping property to WrapWithOverflow.

    Step 2 – Configure The WebBrowser Control For Interaction Between Its Contents And The WPF Application

    To configure the WebBrowser control for interaction between its contents and the WPF application

    The code here is based on what’s outlined in How does WPF WebBrowser Control handle window.external.notify()?.

    1. Open MainWindow.xaml markup for editing by double clicking on it in the Solution Explorer.
    2. Add the following attribute to the WebBrowsercontrol markup:

      Loaded="webBrowser1_Loaded"

    3. Add related event handler to the MainWindow class in MainWindow.xaml.csfile

      private void webBrowser1_Loaded(object sender, RoutedEventArgs e)
      {
          ((WebBrowser)sender).ObjectForScripting = new HtmlInteropClass();
      }

    4. Add related class to the MainWindow.xaml.csfile or in its own file added to the solution with the following implementation:

      [System.Runtime.InteropServices.ComVisibleAttribute(true)]
      public class HtmlInteropClass
      {
          public void Notify(string token)
          {
              ((MainWindow)Application.Current.MainWindow).textBox2.Text = token;

          }
      }

    5. The Notify method will be called by a script hosted in a web page generated by ACS upon successful authentication with selected IdP.
    6. Compile the solution to make sure there are no compilation errors.

    Step 3 – Test Your Solution

    To test your solution

    1. Run your solution by pressing F5.
    2. Click on the button above the ListBox. The ListBox should be populated with the configured IdP’s. Click on the IdP’s in the ListBox. Their related sign in URL’s should appear in the TextBox under the ListBoxwith IdP’s. Here is how mine looks:

      image

    3. Click on the other Button on the right to load sign in page related to the selected IdP. Here is how it looks when loading Live ID sign in page:

      image

    4. Provide your credentials to the sign in web form and click Sing in button in it.
    5. Upon successful authentication you should see the contents of the SWT token received from ACS. Here is mine:

      image

    6. Do the same with other IdP’s where you have active account.
    7. Next you can use the token to submit to your RESTful WCF service and then validate it at the RESTful WCF service end. For the details on how to do it consult the following steps in How To: Authenticate to a REST WCF Service Deployed to Windows Azure Using ACS:
    • Step 3 - Implement Code That Validates the SWT Token at the REST WCF Service
    • Step 4 – Implement a Client That Requests The SWT Token From ACS and Forwards It To The REST WCF Service
  • Alik Levin's

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

    • 0 Comments

    Programming Windows Identity Foundation

    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 = "http://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

  • Alik Levin's

    Windows Azure Service Bus Resources For Under Fire Scenarios

    • 1 Comments

    Mastering Windows Azure Service BusThis is a quick list of resources for the under fire scenarios for Windows Azure Service Bus (Windows Azure is  here and SQL Azure here). Under fire scenarios in my speak is when something needs to be done quickly. Example, fix error, write working code, get up to speed with the folksonomy.

    How-To’s         

    Troubleshooting

    Slides

    Videos

Page 1 of 4 (11 items) 1234