本主题演示如何在 Web 服务器上配置 ASP.NET 身份验证服务,以便客户端可将其用作 Windows Communication Foundation (WCF) 服务。 此外,本主题还演示如何配置 ASP.NET Forms 身份验证。
有关如何配置 ASP.NET 成员资格的更多信息,请参见 配置 ASP.NET 应用程序以使用成员资格。
启用身份验证服务
-
如果还没有 ASP.NET Web 应用程序,请创建一个。
-
向包含以下指令来引用 AuthenticationService 类的网站中添加一个服务文件 (.svc),如下面的示例所示:
<%@ ServiceHost Language="VB" Service="System.Web.ApplicationServices.AuthenticationService" Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
- <%@ ServiceHost
- Language="C#"
- Service="System.Web.ApplicationServices.AuthenticationService"
- Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
-
在 Web.config 文件中进行以下配置设置以配置服务及要求使用 SSL:
-
在 authenticationService 元素中启用身份验证服务。
-
在 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> <authenticationService enabled="true" requireSSL = "true"/> </webServices> </scripting> </system.web.extensions> <system.serviceModel> <services> <service name="System.Web.ApplicationServices.AuthenticationService" behaviorConfiguration="AuthenticationServiceTypeBehaviors"> <endpoint contract= "System.Web.ApplicationServices.AuthenticationService" binding="basicHttpBinding" bindingConfiguration="userHttps" bindingNamespace="http:///ApplicationServices/v200"/> </service> </services> <bindings> <basicHttpBinding> <binding name="userHttps"> <security mode="Transport" /> </binding> </basicHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="AuthenticationServiceTypeBehaviors"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> </system.serviceModel>
-
配置 Forms 身份验证
-
在 Web.config 文件中,配置 Web 应用程序以使用 Forms 身份验证。
下面的示例演示 Web.config 文件中的 authentication 元素,该元素已配置为使用 Forms 身份验证。
<authentication mode="Forms"> <forms cookieless="UseCookies" /> </authentication>
身份验证服务需要 cookie。 因此,在 authentication 元素中,应将 cookieless 特性设置为“UseCookies”。 有关更多信息,请参见 ASP.NET Forms 身份验证概述。