如何:启用 WCF 角色服务

更新:2007 年 11 月

本主题演示如何在 Web 服务器上配置 ASP.NET 角色服务,以便使用 Windows Communication Foundation (WCF) 的客户端可以使用该服务。

Bb398911.alert_note(zh-cn,VS.90).gif说明:

如果角色名称可能被恶意用户用来公开敏感数据,或角色名称存储在持久性 cookie 中,请通过安全套接字层(SSL,通过使用 HTTPS 协议)访问角色服务。有关如何设置 SSL 的更多信息,请参见 Microsoft 网站上的 Configuring Secure Sockets Layer (IIS 6.0 Operations Guide)(配置安全套接字层(IIS 6.0 操作指南))和 Configuring Secure Sockets Layer in IIS 7.0(在 IIS 7.0 中配置安全套接字层)。

将 ASP.NET 角色服务配置为 WCF 服务

  1. 如果还没有 ASP.NET Web 应用程序,请创建一个。

  2. 在 Web 应用程序中启用 Forms 身份验证。有关更多信息,请参见配置 ASP.NET 应用程序以使用成员资格

  3. 在 Web 应用程序中启用角色。有关更多信息,请参见了解角色管理

  4. 为用户提供登录到 Web 应用程序的方法。

    通过 WCF 角色服务检索角色时,还可以通过 WCF 身份验证服务登录用户。有关如何设置 WCF 身份验证服务的信息,请参见如何:启用 WCF 身份验证服务

  5. 将服务文件 (.svc) 添加到包含下列引用 RoleService 类的指令的网站:

    <%@ ServiceHost 
      Language="VB"
      Service="System.Web.ApplicationServices.RoleService" 
      Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
    <%@ ServiceHost 
      Language="C#"
      Service="System.Web.ApplicationServices.RoleService" 
      Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
    
  6. 保存 .svc 文件,并将其关闭。

  7. 在 Web.config 文件中进行以下配置设置以配置服务及要求使用 SSL:

    • 在 roleService 元素中启用角色服务。

    • 在 services 元素中定义终结点协定,在 behaviors 元素中定义服务行为。在终结点协定中包括 bindingNamespace 属性,以防止在某些代理生成工具中出现异常。有关 WCF 终结点的更多信息,请参见 Windows Communication Foundation Endpoints(Windows Communication Foundation 终结点)。

    • 配置 serviceHostingEnvironment 元素以获得 ASP.NET 兼容性。有关如何承载 WCF 服务的更多信息,请参见 WCF Services and ASP.NET(WCF 服务和 ASP.NET)。

    • 在需要 SSL 的 bindings 元素中创建绑定。有关 WCF 中传输安全性的更多信息,请参见 Transport Security(传输安全性)。

    下面的示例演示 Web.config 文件中的 system.serviceModel 元素,该元素演示了上面列出的配置设置。

    <system.web.extensions>
      <scripting>
        <webServices>
          <roleService enabled="true"/>
        </webServices>
      </scripting>
    </system.web.extensions>
    <system.serviceModel>
      <services>
        <service name="System.Web.ApplicationServices.RoleService"
            behaviorConfiguration="ApplicationServiceTypeBehaviors">
          <endpoint contract=
            "System.Web.ApplicationServices.RoleService"
            binding="basicHttpBinding"
            bindingConfiguration="userHttps" 
            bindingNamespace="https://asp.net/ApplicationServices/v200"/>
        </service>
      </services>
      <bindings>
    <basicHttpBinding>
    <binding name="userHttps">
    <security mode="Transport" />
    </binding>
    </basicHttpBinding>
    </bindings>
      <behaviors>
        <serviceBehaviors>
          <behavior name="ApplicationServiceTypeBehaviors">
            <serviceMetadata httpGetEnabled="true"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment
        aspNetCompatibilityEnabled="true"/>
    </system.serviceModel>
    

安全性

如果您使用的是敏感用户数据,请通过安全套接字层(SSL,使用 HTTPS 协议)访问身份验证服务。有关如何设置 SSL 的更多信息,请参见 Microsoft 网站上的 Configuring Secure Sockets Layer (IIS 6.0 Operations Guide(配置安全套接字层(IIS 6.0 操作指南))和 Configuring Secure Sockets Layer in IIS 7.0(在 IIS 7.0 中配置安全套接字层)。

请参见

任务

演练:使用 ASP.NET 应用程序服务

概念

Windows Communication Foundation 角色服务概述

其他资源

Configuring Services(配置服务)