步骤 1:创建自定义安全修整程序

本演练介绍如何使用 Microsoft Visual Studio 2005 创建、部署并注册用于 Microsoft Office SharePoint Server 2007 企业级搜索 的自定义安全修整程序。

步骤 1 介绍如何创建自定义安全修整程序,包括以下任务:

  • 设置自定义安全修整程序项目

  • 编写自定义安全修整程序代码

  • 用强名称编译自定义安全修整程序项目

设置自定义安全修整程序项目

创建用于自定义安全修整程序的项目

  1. 在 Visual Studio 中,在“文件”菜单上, 指向“新建”,然后单击“项目”。

  2. 在“项目类型”中,在“C#”下选择“Windows”。

  3. 在“模板”之下,选择“类库”。在“名称”字段中键入“CustomSecurityTrimmerSample”,然后单击“确定”。

接下来,必须向您的 Web 部件项目添加需要的引用。

向 CustomSecurityTrimmerSample 项目添加引用

  1. 在“项目”菜单上,单击“添加引用”。

  2. 在“.NET”选项卡上,选择以下引用,然后单击“确定”:

    • Microsoft.Office.Server.Search

备注

如果 SharePoint 网站配置为使用表单身份验证,要访问用户名,必须使用 HttpContext 类,因此您必须在此处添加一个对“System.Web”的引用。

为安全修整程序添加代码之前,用新的类文件替换默认的类文件。

为安全修整程序创建类文件

  1. 在解决方案资源管理器中,右键单击“Class1.cs”,然后单击“删除”,以删除该项目创建的默认类。

  2. 在“项目”菜单上,单击“添加新项”。

  3. 在“添加新项”对话框中单击“类”,键入“CustomSecurityTrimmer.cs”,然后单击“添加”。

编写自定义安全修整程序代码

修改 CustomSecurityTrimmer 中的默认代码

  1. 用其他命名空间指令在代码顶部附近添加下列 using 语句。

    using System.Collections;
    using System.Collections.Specialized;
    using System.Security.Principal;
    using Microsoft.Office.Server.Search.Query;
    using Microsoft.Office.Server.Search.Administration;
    
  2. 在类声明中指定 CustomSecurityTrimmer 类实现 ISecurityTrimmer 接口,如下所示。

    public class CustomSecurityTrimmer : ISecurityTrimmer
    

现在,便可开始编写用来实现 ISecurityTrimmer 接口方法的代码了。

实现 ISecurityTrimmer 接口方法

  1. Initialize() 方法声明添加下面的代码。

    public void Initialize(NameValueCollection trimmerProps, SearchContext searchCxt)
    {
    
    }
    

    该示例的基本版本未在 Initialize 方法中包含任何代码。步骤 3(可选):指定所检查的爬网 URL 数的可配置限制中的 Initialize 方法包含一个实现示例。

    有关实现 Initialize 方法的详细信息,请参阅企业级搜索结果的自定义安全修整概述

  2. CheckAccess() 方法声明添加下面的代码。

    public BitArray CheckAccess(IList<String> crawlURLs,IDictionary<String,Object> sessionProperties)
    {
    //CheckAccess method implementation, see steps 3-5.
    }
    
  3. 作为 CheckAccess 方法实现的第一部分,声明并实例化 BitArray 变量,以便在 crawlURLs 集合中存储每个 URl 的访问检查结果,并检索提交该查询的用户的用户名,如下所示。

    BitArray retArray = new BitArray(crawlURLs.Count);
    //For Windows authentication, uncomment the next line:
    //string strUser = WindowsIdentity.GetCurrent().Name;
    //For Forms authentication, uncomment the next line:
    //string strUser = HttpContext.Current.User.Identity.Name;
    
  4. 遍历集合中的每个爬网 URL 并执行访问检查,以确定提交查询的用户是否可以访问爬网 URL 的相关内容项。

    如果用户具有访问该内容项的权限,设置索引中 BitArray 项的值,将 retArray[x] 设为 true;否则,将其设为 false,如下所示。

    for (int x = 0; x < crawlURLs.Count; x++)
    {
    /*
    To fully implement the security trimmer, add code to perform the security check and determine if strUser can access crawlURLs[x]. 
    If strUser can access crawlURL[x], then:
    */
    retArray[x] = true;
    //If not:
    retArray[x] = false;
    }
    
  5. 将 CheckAccess 方法的返回值设为 retArray,如下所示。

    return retArray;
    

用强名称编译自定义安全修整程序项目

用强名称编译自定义安全修整程序项目

  1. 在 Visual Studio 2005 中,在“项目”菜单上单击“CustomSecurityTrimmerSample 属性”。

  2. 在“签名”中,选择“为程序集签名”。

  3. 在“选择强名称密钥文件”中,选择“新建”。在“密钥文件名称”中,键入密钥文件的名称,然后单击“确定”。

    备注

    如果您有可以使用的现有密钥文件,可在这一步选择“浏览”来查找现有文件,而不选择“新建”。

  4. 在“生成”菜单上,单击“生成解决方案”。

后续步骤

步骤 2:部署和注册自定义安全修整程序中,您将部署并注册该自定义安全修整程序。

步骤 3(可选):指定所检查的爬网 URL 数的可配置限制包含修改过的自定义安全修整程序类的版本,向您演示如何在经过自定义安全修整程序检查的众多项目上实现可配置的限制。

示例

以下是本步骤所述 CustomSecurityTrimmerSample 类的完整示例代码。

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server.Search.Query;
using Microsoft.Office.Server.Search.Administration;
//For Windows Authentication
using System.Security.Principal;
//For Forms Authenticaion
using System.Web;
using System.Collections.Specialized;
using System.Collections;

namespace CustomSecurityTrimmerSample
{
    class CustomSecurityTrimmer : ISecurityTrimmer
    {
        public void Initialize(NameValueCollection trimmerProps, SearchContext searchCxt)
        {
        }

        public BitArray CheckAccess(IList<String> crawlURLs, IDictionary<String, Object> sessionProperties)
        {
            BitArray retArray = new BitArray(crawlURLs.Count);
            
        //For Windows authentication, uncomment the next line:
            //string strUser = WindowsIdentity.GetCurrent().Name;
        //For Forms authentication, uncomment the next line:
           //string strUser = HttpContext.Current.User.Identity.Name;

            for (int x = 0; x < crawlURLs.Count; x++)
            {
              /*
                To fully implement the security trimmer,
                add code to perform the security check 
                and determine if strUser can access crawlURLs[x].
                If strUser can access crawlURL[x], then:
               */
                retArray[x] = true;
              //If not:
                retArray[x] = false;
            }
            return retArray;
        }
    }
}

See Also

参考

Microsoft.Office.Server.Search.Query.ISecurityTrimmer

概念

企业级搜索结果的自定义安全修整概述

演练:对搜索结果使用自定义安全修整程序

步骤 2:部署和注册自定义安全修整程序

步骤 3(可选):指定所检查的爬网 URL 数的可配置限制