VS 2008单元测试的WCF安全性错误

时间:2008-10-17 21:58:05

标签: .net wcf unit-testing authentication

我正在使用WCF服务运行我的第一个Visual Studio 2008单元测试,并收到以下错误:

  

测试方法   UnitTest.ServiceUnitTest.TestMyService   抛出异常:   System.ServiceModel.Security.MessageSecurityException:   HTTP请求未经授权   客户认证方案   '匿名'。身份验证标头   从服务器收到了   '协商,NTLM'。 --->   System.Net.WebException:远程   服务器返回错误:(401)   未经授权..

我还在安全日志中收到以下失败的审核:

  

登录失败:原因:用户有   未被授予请求的登录   在这台机器上键入
用户   姓名:(互联网访客帐户)
  域名:
登录类型:3   
登录过程:IIS
  认证包:
  MICROSOFT_AUTHENTICATION_PACKAGE_V1_0
  工作站名称:

我在Windows XP SP3计算机上的IIS 6.0中托管WCF服务。我为WCF服务虚拟目录检查了“匿名访问”和“集成Windows身份验证”。

这是我的服务配置文件:

<system.serviceModel>
    <services>
        <bindings>
            <basicHttpBinding>
                <binding name="MyBinding">
               <security mode="None" />
           </binding>
            </basicHttpBinding>
            <customBinding>
                <binding name="MyBinding">
               <transactionFlow />
                    <textMessageEncoding />
                    <httpsTransport authenticationScheme="Ntlm"/>
                </binding>
            </customBinding>
            <wsHttpBinding>
                <binding name="MyBinding">
                   <security mode="None" />
               </binding>
            </wsHttpBinding>
        </bindings>
        <service 
            behaviorConfiguration="Service1Behavior"
            name="Service1"
        >
            <endpoint 
                address="" 
                binding="wsHttpBinding"
                bindingConfiguration="MyBinding"
                contract="IService1"
            >
                <identity>
                    <dns value="localhost" />
                   </identity>
            </endpoint>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="Service1Behavior">
                <serviceMetadata httpGetEnabled="true" />
                   <serviceDebug includeExceptionDetailInFaults="false" />
               </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

4 个答案:

答案 0 :(得分:5)

我不得不更改以下IIS和WCF服务配置以超越“Negotiate,NTLM”异常。

IIS配置:

  

- 取消选中“匿名访问”复选框,然后选中“集成   Windows身份验证“复选框   的目录安全设置   WCF服务虚拟目录。

WCF服务:

  

- 实现了basicHttpBinding并配置了basicSettingBinding   安全设置为   “TransportCredentialsOnly”模式和   TransportClientCredentialType为   “窗口”

这是我更新的wcf服务配置:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="windowsBasicHttpBinding">
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows" />
                </security>
            </binding>
       </basicHttpBinding>
    </bindings>
    <services>
        <service    
      behaviorConfiguration="CityOfMesa.ApprovalRouting.WCFService.RoutingServiceBehavior"
           name="CityOfMesa.ApprovalRouting.WCFService.RoutingService"
        >
            <endpoint 
                binding="basicHttpBinding" bindingConfiguration="windowsBasicHttpBinding"
                name="basicEndPoint"    
                contract="CityOfMesa.ApprovalRouting.WCFService.IRoutingService" 
            />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior 
                name="CityOfMesa.ApprovalRouting.WCFService.RoutingServiceBehavior"
            >
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
           </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

答案 1 :(得分:2)

如果绑定中有securityMode =“None”,则应关闭集成身份验证。

答案 2 :(得分:1)

默认身份验证是Windows(或NTLM),因此您需要在配置文件中指定不需要身份验证。

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="myBinding">
        <security mode="None" />
      </binding>
  </bindings>
</system.serviceModel>

还将此属性添加到端点

bindingConfiguration="myBinding"

binding元素指定对wsHttpBinding的标准行为的修改。

然后端点上的“bindingConfiguration =”myBinding“属性表明该端点应该使用我们指定的修改。

答案 3 :(得分:0)

作为附注.....有一个GPO设置“NTLM身份验证级别”,它控制了身份验证导致单元测试生成“Negotiate,NTLM”异常的身份验证。