65.9K
CodeProject 正在变化。 阅读更多。
Home

启用 WCF BasicHttpBinding 上 Windows 身份验证的八个步骤

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.87/5 (27投票s)

2009 年 5 月 9 日

CPOL

4分钟阅读

viewsIcon

465860

downloadIcon

5455

通过八个基本步骤,我们可以为 BasicHttpBinding 启用 Windows 身份验证安全。

目录

引言和目标

在本节中,我们将通过八个基本步骤来为 BasicHttpBinding 启用 Windows 身份验证安全。您可以在 WCF 中定义两种安全模式:传输级别和消息级别。在本文中,我们将讨论如何为 BasicHttpBinding 定义传输级别安全。

现在我正在分发我的 400 个问答电子书,其中涵盖了主要的 .NET 相关主题,如 WCF、WPF、WWF、AJAX、核心 .NET、SQL Server、架构等等。我相信您会喜欢这本书: http://www.questpond.com/SampleDotNetInterviewQuestionBook.zip

我的其他 WCF FAQ 文章

第一步:创建 WCF 项目

创建一个 WCF 服务应用程序项目,如下图所示

默认情况下,WCF 项目会创建一个类文件,其中包含 GetData 函数。此函数接受数字值,并在您输入“1”时显示类似“您输入了 1 个值”的解释性语句。

public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
}

第二步:确保身份验证模式为 Windows

当我们创建 WCF 服务应用程序时,它还关联了一个 web.config 文件。因此,请打开 web.config 文件并确保身份验证模式为 Windows。

<authentication mode="Windows" />

第三步:在 web.config 文件中定义绑定

第三步是定义绑定和传输类型。要定义绑定,我们需要在 bindings XML 标签内输入 basicHttpBinding 元素。我们还需要将 clientCredentialType 定义为 Windows。

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpEndpointBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
.........
.........
</system.serviceModel>

第四步:将绑定与服务接口绑定

现在,定义的绑定需要与服务接口关联,即 service1。因此,我们需要按如下所示修改 services 元素。您会注意到我们定义了一个具有绑定关联的终结点。

<system.serviceModel>
........
........
........
<services>
<service behaviorConfiguration="WCFWindowsBasicHttpBinding.Service1Behavior" 
                       name="WCFWindowsBasicHttpBinding.Service1">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="BasicHttpEndpointBinding"
name="BasicHttpEndpoint" contract="WCFWindowsBasicHttpBinding.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
.........
.........
.........
.........
</system.serviceModel>

总而言之,您的 <system.serviceModel> XML 部分(包括绑定和服务)如下所示

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpEndpointBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="WCFWindowsBasicHttpBinding.Service1Behavior" 
               name="WCFWindowsBasicHttpBinding.Service1">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="BasicHttpEndpointBinding"
name="BasicHttpEndpoint" contract="WCFWindowsBasicHttpBinding.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFWindowsBasicHttpBinding.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below 
               to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below 
              to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

第五步:确保禁用匿名访问

转到 IIS 属性,然后单击“安全”选项卡,并确保禁用匿名访问,并且仅启用 Windows 身份验证。

第六步:将 WCF 服务托管在 IIS 上

我们需要在 IIS 中托管我们的服务。将目录设置为 IIS 应用程序,以便可以托管您的服务。现在,如果您尝试浏览该服务,即 SVC 文件,您会看到它会弹出身份验证授权安全对话框。因此,此服务无法通过 Windows 身份验证执行。

第七步:消费 WCF 服务

让我们来消费 WCF 服务。添加一个 ASP.NET Web 应用程序并添加 Web 引用。您将看到一个对话框,如下所示。单击“添加引用”以生成 WCF 服务的代理。

第八步:创建 WCF 客户端

在页面加载中输入以下代码片段。添加命名空间引用并调用 GetData 方法。要特别注意的步骤是提供的凭据。DefaultCredentials 将当前 Windows 标识传递给 WCF 服务。

如果您执行服务,您应该会看到如下显示,如以下图所示

您可以尝试在客户端中注释掉以下代码,也就是说,我们没有传递任何凭据。

obj.Credentials = System.Net.CredentialCache.DefaultCredentials;

现在,如果您执行,您应该会收到以下错误,表明这是一个未经授权的调用。

如需进一步阅读,请观看以下面试准备视频和分步视频系列。

© . All rights reserved.