WCF Windows身份验证,本地调试正常但无法在服务器上运行

时间:2014-04-12 08:57:06

标签: c# wcf asp.net-mvc-4 iis-7 windows-authentication

在我的服务器上,运行IIS 7中托管的Windows身份验证WCF应用程序:

IIS配置:Windows身份验证:已禁用;匿名:启用

<system.serviceModel>
    <services>
        <service name="Services.AccountService" behaviorConfiguration="MyServiceTypeBehaviors">
        <endpoint address="" binding="ws2007HttpBinding" bindingConfiguration="AccountServiceBinding"
        contract="Contracts.IAccountService" />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyServiceTypeBehaviors" >
        <!--<serviceDebug includeExceptionDetailInFaults="true"/>-->
                <serviceMetadata  httpGetEnabled="true" httpGetUrl="" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <bindings>
        <ws2007HttpBinding>
            <binding name="AccountServiceBinding" >
                <security mode="Message">
                    <message clientCredentialType="Windows" />
                </security>
            </binding>
        </ws2007HttpBinding>
    </bindings>
</system.serviceModel>

然后我用以下代码创建一个MVC4应用程序来调用WCF服务:

WS2007HttpBinding myBinding = new WS2007HttpBinding(); 
myBinding.Security.Mode = SecurityMode.Message;
EndpointAddress endpoint = new EndpointAddress("http://server/accountservice.svc");

AccountServiceClient _client = new AccountServicesObjects.AccountServiceClient(myBinding, endpoint);
_client.ClientCredentials.Windows.ClientCredential.UserName = "username";
_client.ClientCredentials.Windows.ClientCredential.Password = "password";

var user = _client.GetUserInformation(); // works fine

完成此mvc4应用程序后,我将此网站部署到运行WCF的同一服务器,当我登录时,发生:

System.ServiceModel.Security.SecurityNegotiationException: The caller was not authenticated by the service. 
System.ServiceModel.FaultException:The request for security token could not be satisfied because authentication failed. 
on System.ServiceModel.Security.SecurityUtils.ThrowIfNegotiationFault(Message message, EndpointAddress target)
on System.ServiceModel.Security.SspiNegotiationTokenProvider.GetNextOutgoingMessageBody(Message incomingMessage, SspiNegotiationTokenProviderState sspiState)

这是怎么发生的?

1 个答案:

答案 0 :(得分:0)

看来你的服务还没有开始。如果启用跟踪,则可以找到异常。您的配置中存在错误 - 您没有基址,但您已将HttpGetUrl设置为true(对于该选项,您必须设置基址)

这应该适合你:

<service name="Services.AccountService" behaviorConfiguration="MyServiceTypeBehaviors">
    <endpoint address="accountservice.svc" binding="ws2007HttpBinding" 
        bindingConfiguration="AccountServiceBinding"
        contract="Services.IAccountService" />
    <host>
          <baseAddresses>
                 <add baseAddress="http://localhost:8000/"/>
          </baseAddresses>
    </host>
</service>