Sample 1: Writing a Claims Provider

Applies to: SharePoint Foundation 2010

Claims Provider Sample

To write a claims provider, your first step is to create a class that derives from the SPClaimProvider class. The following sample shows how to write a claims provider. This sample implementation does not support entity, hierarchy, resolve, or search. This topic assumes that you have read the How to: Create a Claims Provider topic.

For more information about creating a claims provider and for a walkthrough, see Claims Walkthrough: Writing a Claims Provider.

Tip

For additional code examples and more information about the SPClaimProvider class and its members, see SPClaimProvider. Also, check the SharePoint SPIdentity Team Blog and the Share-n-dipity blog regularly for additional samples and updates.

using System;
using System.Collections.Generic;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Claims;
using Microsoft.SharePoint.Diagnostics;

namespace MySample.Sample.Server.SampleClaimsProvider
{
    /// <summary>
    /// The SampleNameIdClaimsProvider class is a claims provider for an security token service(STS).
    /// This claims provider inserts a NameIdentifier 
    /// (https://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier)
    /// claim type in the SAML token issued by the STS.
    /// The value of this claim type is the SharePointID of the user requesting the SAML token.
    /// </summary>
    
    [Microsoft.SharePoint.Security.SharePointPermission(System.Security.Permissions.SecurityAction.Demand, ObjectModel = true)]
    [Microsoft.SharePoint.Security.SharePointPermission(System.Security.Permissions.SecurityAction.LinkDemand, ObjectModel = true)]    

public sealed class SampleNameIdClaimsProvider : SPClaimProvider
    {
        #region Constructor
        /// <summary>
        /// Constructor for the SampleNameIdClaimsProvider class. It sets the displayName
        /// of the claims provider, which is displayed in the Central Administration user interface for
        /// people picker name resolution.
        /// </summary>
        /// <param name="displayName">String that gets displayed in the Central Administration user interface 
        /// for people picker name resolution.</param>

        public SampleNameIdClaimsProvider (string displayName) : base(displayName)
        {          
        }

        #endregion Constructor

        #region Private Methods/Properties
        /// <summary>
        /// Returns the URI of the SampleNameIdClaimsProvider claim.
        /// </summary>
        
       /// <returns>String representing the URI for a claim that specifies the name of an entity.</returns>
        private static string SampleNameIdClaimType
        {
            get{ return "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier"; }
        }

        /// <summary>
        /// Returns the value type of the SampleNameIdClaimsProvider claim.
        /// </summary>
        /// <returns>String representing the value type of the NameIdentifier claim.</returns>
        private static string SampleNameIdClaimValueType
        {
            get{ return Microsoft.IdentityModel.Claims.ClaimValueTypes.String; }
        }
     
       
        #endregion Private Methods/Properties 

        #region Protected Methods
        
        /// <summary>
        /// This is the main function of the SampleNameIdClaimsProvider.
        /// It creates a SampleNameId claim, sets SharePointID as its value,
        /// and then adds this claim to the SPClaim list claims.
        /// </summary>
        /// <param name="context">URI context of the request.</param>
        /// <param name="entity">SharePointID of the entity requesting the claim.</param>
        /// <param name="claims">SPClaim generic list where SampleNameId claim is added.</param>
        /// <returns>void</returns>
        protected override void FillClaimsForEntity(Uri context, SPClaim entity, List<SPClaim> claims)
        {
            
            if (null == entity)
            {
                throw new ArgumentNullException("entity");
            }
            if(null == claims)
            {                
               throw new ArgumentNullException("claims");
            }

            //Adding the SampleNameId claims to the claims list and setting SharePointID as its value.
                          
               claims.Add(CreateClaim(SampleNameIdClaimType, entity.Value, SampleNameIdClaimValueType));   

        }

        /// <summary>
        /// This function adds the claims types that will be added from this claims provider.
        /// </summary>        
        /// <param name="claimTypes">String generic list where claims URIs will be added.</param>
        /// <returns>void</returns>
        protected override void FillClaimTypes(List<string> claimTypes)
        {

            if(null == claimTypes)
            {              
                throw new ArgumentNullException("claimTypes");
            }
            
            // Add the claim types that will be added by this claims provider.          
                claimTypes.Add(SampleNameIdClaimType);
            
        }

        /// <summary>
        /// This method adds the valueTypes of the claimTypes that will be placed
        /// into the SAML token.
        /// Note: The claimValueTypes should be in the same order as the claimTypes.
        /// </summary>
        /// <param name="claimValueTypes>List where claim value types will be added.</param>
        /// <returns>void</returns>
        protected override void FillClaimValueTypes(List<string> claimValueTypes)
        {

            if(null == claimValueTypes)
            {              
                throw new ArgumentNullException("claimValueTypes");
            }
            
                //Adding the SampleNameId claim value type.
                claimValueTypes.Add(NameIdentifierClaimValueType);
            };

        #region Non-Implemented
        /// <summary>
        /// This function adds all the entity types that this claims provider will
        /// be supporting for people picker. In this example, this functionality is not supported.
        /// </summary>
        
        protected override void FillEntityTypes(List<string> entityTypes)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// This function adds the hierarchy to the hierarchy tree. This functionality is also
        /// used for people picker. In this example, this functionality is not supported.
        /// </summary>
        
        protected override void FillHierarchy(Uri context, string[] entityTypes, string hierarchyNodeID, int numberOfLevels, Microsoft.SharePoint.WebControls.SPProviderHierarchyTree hierarchy)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// This method is used to resolve multiple claims. This functionality is also
        /// used for people picker. In this example this functionality is not supported.        
        /// </summary>
        
        protected override void FillResolve(Uri context, string[] entityTypes, SPClaim resolveInput, List<Microsoft.SharePoint.WebControls.PickerEntity> resolved)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// This method is used to resolve multiple claims. This functionality is also
        /// used for people picker. In this example, this functionality is not supported.
        /// </summary>

        protected override void FillResolve(Uri context, string[] entityTypes, string resolveInput, List<Microsoft.SharePoint.WebControls.PickerEntity> resolved)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// This method is used to fill schema. This functionality is also
        /// used for people picker. In this example, this functionality is not supported.        
        /// </summary>

        protected override void FillSchema(Microsoft.SharePoint.WebControls.SPProviderSchema schema)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// This method is used to enable search. This functionality is also
        /// used for people picker. In this example, this functionality is not supported.
        /// </summary>
        
        protected override void FillSearch(Uri context, string[] entityTypes, string searchPattern, string hierarchyNodeID, int maxCount, Microsoft.SharePoint.WebControls.SPProviderHierarchyTree searchTree)
        {
            throw new NotImplementedException();
        }
        #endregion NULL-Implementation
        #endregion Protected Methods


        #region Public Methods

        /// <summary>
        /// Returns the name of the claims provider. This name should be unique and you
        /// must ensure that this name does not clash with the existing claims provider.
        /// </summary>
        /// <returns>String containing unique name for the claims provider.</returns>

        public override string Name
        {
            get{ return SampleNameIdClaimProvider.SampleClaimProviderName; }
        }
        
        /// <summary>
        /// Returns the name of the claims provider. This name should be unique and you
        /// must ensure that this name does not clash with the existing claims provider.
        /// </summary>
        
        /// <returns>String containing unique name for the claim provider.</returns>
        internal static string SampleClaimProviderName
        {
            get{ return "SampleClaimsProvider"; }
        }

        /// <summary>
        /// Informs whether the claims provider supports entity information. The claims provider
        /// infrastructure adds the claims only if this SupportsEntityInformation property is true.
        /// </summary>
        
        /// <returns>true, representing entity information is supported.</returns>
        public override bool SupportsEntityInformation
        {
            get{ return true; }
        }

        /// <summary>
        /// Informs whether hierarchy is supported. This is used for people picker functionality.
        /// In this example, this functionality is not supported; therefore it is set to false.
        /// </summary>
        
        /// <returns>false, representing entity information is not supported.</returns>
        public override bool SupportsHierarchy
        {
            get{ return false; }
        }

        /// <summary>
        /// Informs whether resolve entity feature is supported. This is used for people picker functionality.
        /// In this example, this functionality is not supported; therefore it is set to false.
        /// </summary>
        /// <returns>false, representing entity information is not supported.</returns>
        public override bool SupportsResolve
        {
            get{ return false; }
        }

        /// <summary>
        /// Informs whether search functionality is supported on the basis of claims value.
        /// In this example, this functionality is not supported; therefore it is set to false.
        /// </summary>
        
        /// <returns>false, representing search is not supported.</returns>
        public override bool SupportsSearch
        {
            get{ return false; }
        }

       #endregion Public Methods
    }
}