Skip to main content
 
Go Search
Home
Categories
Bloggers
How To Create a Claims Viewer Web Part for SharePoint 2010
By: Travis Nielsen | Posted: December 29, 2009 at 10:53 PM

For the past month or so I’ve been giving Windows Identity Foundation a very close look, especially where it relates to SharePoint 2010. I’ll be posting a series of blogs on this subject and I thought it might be useful to start off with a quick introduction on how to access the claims from which an SPUser object is derived. I feel this is very helpful for folks who are looking to learn more about that what this stuff is all about. As they say, seeing is believing!

The good news is this is fairly easy to do since the SharePoint development tools provided in Visual Studio 2010 do all the heavy lifting for you with respect to solution creation and deployment. By now, I’m sure you’re already aware of this so I won’t spend much time on these features. For this article, I’m assuming you’ll be creating this web part on a development server (or virtual machine) that has both Visual Studio 2010 and SharePoint 2010 installed.

You start by creating a new project with the Visual Web Part template. Feel free to name the solution whatever you wish.

image

Using the split or design view, drag and drop a GridView control onto the .ascx page. In this example, I’ll just use the default name of “GridView1”.

Next, switch to the code-behind for the user control and add a reference to Microsoft.IdentityModel. IMPORTANT: If you have multiple versions of this .dll installed, be sure to reference version 1.0.0.0. This is the version SharePoint 2010 uses and therefore the version we must use.

Add the following code to the user control (.ascx.cs ). Its based on a sample provided on this MSDN blog a while back.

Code Snippet
  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.Web.UI.WebControls.WebParts;
  5. using Microsoft.IdentityModel.Claims;
  6. namespace ClaimsViewerTest.VisualWebPart1
  7. {
  8.     public partial class VisualWebPart1UserControl : UserControl
  9.     {
  10.         protected void Page_Load(object sender, EventArgs e)
  11.         {
  12.             IClaimsPrincipal claimsPrincipal = Page.User as IClaimsPrincipal;
  13.             IClaimsIdentity claimsIdentity = (IClaimsIdentity)claimsPrincipal.Identity;
  14.             GridView1.DataSource = claimsIdentity.Claims;
  15.             Page.DataBind();
  16.         }
  17.     }
  18. }

Lines 16 and 17 bind the claims data to the grid view control. Next, simply hit F5 to deploy and debug. Once you’re satisfied the web part is functional, I recommend adding it to the default page of a site collection within a claims-enabled web application. When signing in, you’ll see something like the following:

image

That’s it!


  Comments   Add Comment   Share It  
  Your Name:
  Your Email: **will not be displayed
  Comment Title:
* Comments:
  If you cannot read the code, please
click here to get a new one. You won't
lose your comments by doing so.
* Security Code:
   
  
  
* Your Name:
* Your Email: **will not be displayed
* Recipient's Email:
* Subject:
  If you cannot read the code, please
click here to get a new one. You won't
lose your comments by doing so.
* Security Code:
  
  
  
asdfasdfasdf
By: sdfsdf | Posted: July 13, 2010 at 9:45 PM
asdfasdfsdf
IT Pro (non-developer)
By: MSBob | Posted: July 13, 2010 at 9:45 PM
Is there a way to get Web Part without Visual Studio 2010? I am just a IT Pro and not a developer.
Odd problem with IClaimsPrincipal
By: Dave | Posted: May 6, 2010 at 2:13 PM
The webpart was throwing an unreferenced object exception. Debugging through the two lines of code I can see that claimsPrincipal remains null after executing the following: IClaimsPrincipal claimsPrincipal = Page.User as IClaimsPrincipal; In the debugger I can actually drill down in the properties of Page.User all the way to the claims. What would cause the cast and assignment to just "not work"? This has me completely baffled but it's probably something stupid. On the box I have SP 2010 and WIF installed. Microsoft.IdentityModel is v3.5. Any ideas?
Re:Smallest necessary tool?
By: Travis | Posted: April 17, 2010 at 1:36 PM
Pamela: This is a web part, so you can use any development tool you're comfortable with to create the solution (.wsp) and deploy it. I use Visual Studio 2010 becuase of the out-of-the-box integration and ease of deployment.
Smallest necessary tool?
By: Pamela | Posted: April 16, 2010 at 5:43 PM
Hi, I really don't want to deploy all of VS 2010 just to get a claims viewer. Can I do this in powershell, or using the express versions of visual studio?
Slight Mistake
By: Shailen Sukul | Posted: April 7, 2010 at 10:26 PM
You need to add version 3.5.0.0 of the Microsoft.IdentityModel library to make it work. Otherwise, really nice work!!!
 

 About Travis Nielsen

ArchitectTravis Nielsen is an architect and founding member of PointBridge. He has over 12 years of experience in the IT industry designing and implementing solutions for the Windows Server platform throughout... [more]

 Tag Cloud

 External Links

 ‭(Hidden)‬ Admin Links