Your First InfoCard WebPart (CardSpace WebPart Example)
As discussed in a previous post, I was talking about a current client I am working with where we developed a bunch of ASP.NET 2.0 WebParts that are related to InfoCard management and provisioning used within SharePoint 2007. Some of this WebParts were strictly for interrogating the InfoCard properties in order to understand what was really going on beyond the scenes with SharePoint and InfoCard interaction. There are a bunch of ways to display the things that InfoCard is doing (such as claims), but there were some very specific things that I was interested in that I wanted to display in SharePoint that revolved around trying to understand what properties where doing what, claim interaction, etc. etc. etc.
Obviously, if you aren’t using CardSpace for your ASP.NET 2.0 Web Applications, this code is not going to prove real helpful. The code itself is very, very simplistic, since we are just going to be showing the associated claims. Within this, we are also going to include the PPID of the user since it is what really uniquely identifies the user. We have talked about the PPID before, it is unique in that it is a hash resulting from a calculation of the issued certificate as well as a unique attribute about the user.
My intention in this post is to introduce SharePoint developers to working with InfoCard objects / the Identity Model’s API’s for the first time, which hopefully leads to amazing development initiatives all over the world :-) The CardSpace controls are a little bit more complex than this example, but this was just for fun on an afternoon.
The first thing that we have to do is establish the appropriate using directives, we are going to be using the System.IdentityModel.Claims and Microsoft.IdentityModel.TokenProcessor namespaces, in the code so that we can tap into the OM that will provide us with the CardSpace insight we desire. You will also need to establish the references in the Visual Studio.NET assemblies since they will provide the appropriate OM access we need.. We are also going to use the TokenProcessor class , which is available with the CardSpace SDK as sample code. The TokenProcessor class is responsible for the decryption and verification of the security token, which we are going to have to do in this WebPart as we are going to be working with the related claims.
- using System;
- using System.Runtime.InteropServices;
- using System.Web.UI;
- using System.Xml.Serialization;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.IdentityModel.Claims;
- using Microsoft.IdentityModel.TokenProcessor;
Once the references are established for the WebPart, we can get started on the WebPart code.
Firstly, ensure that the master class file is inheriting from the System.Web.UI.WebControls.WebParts.WebPart base class so that this can be rendered within a SharePoint environment.
- public class ClaimInterrogationWebPart : WebPart
I am going to be displaying the token values through the use of a ASP.NET Label controls, its just easier since they have a text property that is easy to assign.
- private Label lblGivenName;
- private Label lblEmailAddy;
- private Label lblPPID;
I am going to setup some of the default behavior of the WebPart within the WebPart class constructor. This is not required by any means.
- // Set some of the default WebPart beahvior in the Webpart class constructor
- public ClaimInterrogationWebPart()
- this.ExportMode = WebPartExportMode.All;
- this.ChromeState = PartChromeState.Normal;
- this.ChromeType = PartChromeType.None;
- this.AllowClose = true;
- this.AllowEdit = true;
- this.AllowMinimize = true;
Our WebPart is going to get access to the related claims that are included in a token. Therefore, we are going to have to use the Claims property in order to pool the claims values. Then, we can use the Claims property for an indexed Claim accessor to get the Given Name, Email, and PPID of the user.
- protected override void CreateChildControls()
- string xmlToken;
- xmlToken = Page.Request.Params["xmlToken"];
- Token token = new Token(xmlToken);
- this.lblGivenName = new Label();
- lblGivenName.Text = token.Claims[ClaimTypes.GivenName];
- this.lblEmailAddy = new Label();
- lblEmailAddy.Text = token.Claims[ClaimTypes.Email];
- this.lblPPID = new Label();
- lblPPID.Text = token.Claims[ClaimTypes.PPID];
Lastly, in the render method just make sure that the controls that you were adding to the stream in CreateChildControls are added to the output control collection.
- protected override void Render(HtmlTextWriter writer)
That’s it! Your first CardSpace WebPart. Albeit, a really simple one. At least you got to experience a little bit of CardSpace programming.
For deployment purposes, if you really wanted to do, you could then use a SharePoint feature file. This would involve the elementManifest.xml and feature.xml file.
Feature.xml File For CardSpace ClaimInterrogationWebPart
- <Feature Title="ClaimInterrogationWebPart" Id="98c31c09-74ba-401d-ac9c-4906cd2b977b" Description="" Version="220.127.116.11" Scope="Site" Hidden="FALSE" DefaultResourceFile="core" xmlns="http://schemas.microsoft.com/sharepoint/">
- <ElementManifest Location="elementManifest.xml" />
- <ElementFile Location="ClaimInterrogation.webpart" />
elementManifest.xml File For CardSpace ClaimInterrogationWebPart
- <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
- <Module Name="WebParts" List="113" Url="_catalogs/wp" >
- <File Url="ClaimInterrogation.webpart" Type="GhostableInLibrary" />
In later posts, where I introduce the CardSpace controls, we will dip into a little more hardcore CardSpace programming, as this was only a brief introduction for WebPart developers :-)