安全性验证

ServiceValidation 示例演示如何使用自定义行为验证计算机上的服务,以确保它们符合特定条件。 在此示例中,服务通过扫描服务上的每个终结点并检查它们是否包含安全绑定元素来验证自定义行为。 此示例基于入门指南

注释

本示例的设置过程和生成说明位于本主题末尾。

终结点验证自定义行为

通过将用户代码添加到Validate接口中包含的IServiceBehavior方法,可以赋予服务或端点自定义行为,以执行用户定义的操作。 以下代码用于遍历服务中包含的每个终结点,并在它们的绑定集合中搜索安全绑定。

public void Validate(ServiceDescription serviceDescription,
                                       ServiceHostBase serviceHostBase)
{
    // Loop through each endpoint individually, gathering their
    // binding elements.
    foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
    {
        secureElementFound = false;

        // Retrieve the endpoint's binding element collection.
        BindingElementCollection bindingElements =
            endpoint.Binding.CreateBindingElements();

        // Look to see if the binding elements collection contains any
        // secure binding elements. Transport, Asymmetric, and Symmetric
        // binding elements are all derived from SecurityBindingElement.
        if ((bindingElements.Find<SecurityBindingElement>() != null) || (bindingElements.Find<HttpsTransportBindingElement>() != null) || (bindingElements.Find<WindowsStreamSecurityBindingElement>() != null) || (bindingElements.Find<SslStreamSecurityBindingElement>() != null))
        {
            secureElementFound = true;
        }

    // Send a message to the system event viewer when an endpoint is deemed insecure.
    if (!secureElementFound)
        throw new Exception(System.DateTime.Now.ToString() + ": The endpoint \"" + endpoint.Name + "\" has no secure bindings.");
    }
}

将以下代码添加到 Web.config 文件中,这样服务即可识别 serviceValidate 行为扩展。

<system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="endpointValidate" type="Microsoft.ServiceModel.Samples.EndpointValidateElement, endpointValidate, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </behaviorExtensions>
    </extensions>
    ...
</system.serviceModel>

将行为扩展添加到服务后,现在可以将 endpointValidate 行为添加到 Web.config 文件中的行为列表,从而将行为添加到服务。

<behaviors>
    <serviceBehaviors>
        <behavior name="CalcServiceSEB1">
            <serviceMetadata httpGetEnabled="true" />
            <endpointValidate />
        </behavior>
    </serviceBehaviors>
</behaviors>

添加到 Web.config 文件的行为及其扩展将行为应用于各个服务,而添加到 Machine.config 文件时,会将行为应用于计算机上活动的每个服务。

注释

向所有服务添加行为时,建议在进行任何更改之前备份 Machine.config 文件。

现在运行此示例的 client\bin 目录中提供的客户端。 引发异常,并显示以下消息:“无法激活请求的服务 http://localhost/servicemodelsamples/service.svc 。这是预期的,因为终结点验证行为被视为不安全的终结点,并阻止服务启动。 该行为还会引发内部异常,该异常描述哪个终结点不安全,并将消息写入“System.ServiceModel 4.0.0.0”源和“WebHost”类别下的系统事件查看器。 在此示例中,也可以开启服务的跟踪功能。 这样,用户就可以通过使用服务跟踪查看器工具打开生成的服务跟踪来查看终结点验证行为引发的异常。

在事件查看器中查看失败的终结点验证异常消息

  1. 单击“ 开始 ”菜单,然后选择“ 运行”。

  2. 键入 eventvwr 并单击“ 确定”。

  3. 在“事件查看器”窗口中,单击“ 应用程序”。

  4. 双击 应用程序 窗口中“WebHost”类别下的最近添加的“System.ServiceModel 4.0.0.0”事件,以查看不安全的终结点消息。

设置、生成和运行示例

  1. 确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。

  2. 若要生成解决方案的 C# 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。

  3. 若要在单台计算机或跨计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行操作。

另请参阅