示例 1:编写声明提供程序

上次修改时间: 2010年8月3日

适用范围: SharePoint Foundation 2010

声明提供程序示例

若要编写声明提供程序,应首先创建一个从 SPClaimProvider 类派生的类。下面的示例演示如何编写声明提供程序。此示例实现不支持实体、层次结构、解析或搜索。本主题假定您已经阅读了如何:创建声明提供程序主题。

有关创建声明提供程序的详细信息以及要获取演练,请参阅声明演练:编写声明提供程序

提示提示

有关其他代码示例和 SPClaimProvider 类及其成员的详细信息,请参阅 SPClaimProvider。此外,请定期查看 SharePoint SPIdentity 团队博客(该链接可能指向英文页面) 以及 Share-n-dipity(该链接可能指向英文页面) 博客,以获取其他示例和更新。

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
    }
}