WCF 错误自动生成的客户端和配置中的目标主体名称不正确

WCF Error The target principal name is incorrect from Autogenerated client and configuration

我正在使用 Visual Studio 2013,.NET4.5。

最初我是WCF服务消费者,但由于处理子系统的团队缺乏资源,我接手了WCF服务双方的开发。所以我在本地获得了我需要的 WCF 服务代码 运行。通过删除旧服务参考然后使用 Visual Studio UI 添加 New Service Reference 来插入它。它为我生成了客户端和配置。

它构建并运行良好,但是当我调用该服务时出现错误:

C# 代码:(这在这里几乎无关紧要,但无论如何都会有人要求它)

public class LeafManager
{
    public LeafManager(ICorePricingService pricing, IWebQuoteService quote, IWebPaymentService payment)
    {
        this.Pricing = pricing;
        this.Quote = quote;
        this.Payment = payment;
    }

    public IWebPaymentService Payment { get; set; }

    public IWebQuoteService Quote { get; set; }

    public ICorePricingService Pricing { get; private set; }

    public List<Core.WillProductValuation> GetWillProducts(bool? isMirror, List<Core.QuestionAnswer> questions,
        List<string> discountCodes)
    {
        var result = this.Pricing.GetWillsPrices(new WillPricingParameters{Questions = questions.ToLeafPayment(), DiscountCodes = discountCodes.ToArray(), IsCouple = isMirror}); 
        return result.ToLocal();
    }
...
}

Web.config(VS2013生成)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="ConveyancingEndpoint">
          <security mode="Transport" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://localhost:44301/CorePricingService.svc"
        binding="wsHttpBinding" bindingConfiguration="ConveyancingEndpoint"
        contract="ServiceReference2.ICorePricingService" name="ConveyancingEndpoint">
        <identity>
          <userPrincipalName value="matas.vaitkevicius@group......co.uk" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>

异常:

{"The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'Negotiate oYIETTCCBEmgAwoBA...'."}

服务器堆栈跟踪:

   at System.ServiceModel.Channels.HttpChannelUtilities.ValidateAuthentication(HttpWebRequest request, HttpWebResponse response, WebException responseException, HttpChannelFactory`1 factory)
   at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory`1 factory, WebException responseException, ChannelBinding channelBinding)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at XXX.Legal.Webservices.ServiceReference2.ICorePricingService.GetWillsPrices(WillPricingParameters willPricingParameters)
   at XXX.Legal.Webservices.ServiceReference2.CorePricingServiceClient.GetWillsPrices(WillPricingParameters willPricingParameters) in c:\XXX.Legal\XXX.Legal\XXX.Legal.Webservices\Service References\ServiceReference2\Reference.cs:line 673
   at XXX.Legal.Webservices.LeafManager.GetWillProducts(Nullable`1 isMirror, List`1 questions, List`1 discountCodes) in c:\XXX.Legal\XXX.Legal\XXX.Legal.Webservices\LeafManager.cs:line 26
   at XXX.Legal.Website.Areas.Wills.Controllers.JourneyController.Quote() in c:\XXX.Legal\XXX.Legal\XXX.Legal.Website\Areas\Wills\Controllers\JourneyController.cs:line 90
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()

内部异常:

{"The remote server returned an error: (401) Unauthorized."}

堆栈跟踪:

at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

更多内部异常:

{"The target principal name is incorrect"}

堆栈跟踪:

   at System.Net.NTAuthentication.GetOutgoingBlob(Byte[] incomingBlob, Boolean throwOnError, SecurityStatus& statusCode)
   at System.Net.NTAuthentication.GetOutgoingBlob(String incomingBlob)
   at System.Net.NegotiateClient.DoAuthenticate(String challenge, WebRequest webRequest, ICredentials credentials, Boolean preAuthenticate)
   at System.Net.NegotiateClient.Authenticate(String challenge, WebRequest webRequest, ICredentials credentials)
   at System.Net.AuthenticationManager.Authenticate(String challenge, WebRequest request, ICredentials credentials)
   at System.Net.AuthenticationState.AttemptAuthenticate(HttpWebRequest httpWebRequest, ICredentials authInfo)
   at System.Net.HttpWebRequest.CheckResubmitForAuth()
   at System.Net.HttpWebRequest.CheckResubmit(Exception& e, Boolean& disableUpload)

如果我们查看最里面的异常,它指出 The target principal name is incorrect 这意味着 (WCF) 生成了不正确的 UPN。我一定是遗漏了一些明显的东西。

我自己想出来了。

我需要做的就是删除

   <identity>
      <userPrincipalName value="matas.vaitkevicius@group......co.uk" />
    </identity>

来自 Web.config