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

Mic*_*ern 3 .net authentication wcf unit-testing

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

测试方法UnitTest.ServiceUnitTest.TestMyService引发异常:System.ServiceModel.Security.MessageSecurityException:HTTP请求未经授权,客户端身份验证方案为"Anonymous".从服务器收到的身份验证标头是"Negotiate,NTLM".---> System.Net.WebException:远程服务器返回错误:(401)未经授权..

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

登录失败:原因:未在此计算机上授予用户请求的登录类型
用户名:( Internet来宾帐户)
域:
登录类型: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>
Run Code Online (Sandbox Code Playgroud)

Mic*_*ern 5

我不得不更改以下IIS和WCF服务配置以通过"Negotiate,NTLM"异常.

IIS配置:

- 取消选中"匿名访问"复选框,并选中WCF服务虚拟目录的目录安全性设置中的"集成Windows身份验证"复选框.

WCF服务:

- 实现basicHttpBinding并将basicSettingBinding安全设置配置为"TransportCredentialsOnly"模式,并将TransportClientCredentialType配置为"Windows"

这是我更新的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>
Run Code Online (Sandbox Code Playgroud)