大网数据广告活动
.NET 与 C# 开发

安全性验证

· 52CE 我爱测

此示例演示如何使用自定义行为来验证计算机上的服务,以确保服务符合特定条件。 在此示例中,自定义行为通过以下方法验证服务:扫描服务上的每个终结点,并查看这些终结点是否包含安全的绑定元素。 此示例基于Getting Started。

此示例演示如何使用自定义行为来验证计算机上的服务,以确保服务符合特定条件。 在此示例中,自定义行为通过以下方法验证服务:扫描服务上的每个终结点,并查看这些终结点是否包含安全的绑定元素。 此示例基于Getting Started。

Note: 本主题的最后介绍了此示例的设置过程和生成说明。

终结点验证自定义行为

通过将用户代码添加到 Validate 接口中包含的 xref:System.ServiceModel.Description.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>  
...  

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

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

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

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

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

查看事件查看器中失败的终结点验证异常消息
  1. 单击启动菜单,然后选择运行....

  2. 类型eventvwr然后单击确定。

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

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

设置、生成和运行示例
  1. 请确保您具有执行的 Windows Communication Foundation 示例的一次性安装过程。

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

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

Important: 您的计算机上可能已安装这些示例。 在继续操作之前,请先检查以下(默认)目录:

<InstallDrive>:\WF_WCF_Samples

如果此目录不存在,请转到Windows Communication Foundation (WCF) 和.NET Framework 4 的 Windows Workflow Foundation (WF) 示例若要下载所有 Windows Communication Foundation (WCF) 和WF示例。 此示例位于以下目录:

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Management\ServiceValidation

请参阅

历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。

来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站展开固定版本仓库内的引用内容,将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。

资料来源与适用范围

资料来源
Microsoft / .NET 中文文档历史快照
适用范围
历史中文快照 2019-08-18;详见原文产品版本;文档日期 03/30/2017
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
CC BY 4.0(文档)/ MIT(示例代码)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。