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

本文关键字:服务器 运行 身份验证 Windows WCF 调试 | 更新日期: 2023-09-27 17:58:04

在我的服务器上,运行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)

这是怎么发生的?

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

您的服务似乎根本没有启动。如果启用跟踪,则可以找到异常。您的配置中有一个错误-您没有基本地址,但您已将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>