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

标签 c# wcf visual-studio-2013 web-config .net-4.5

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

原本我是WCF服务使用者,但由于处理子系统的团队缺乏资源,我接管了WCF服务双方的开发。所以我得到了我需要在本地运行它的 WCF 服务的代码。通过删除旧的服务引用,然后使用 Visual Studio UI 添加新服务引用来插入它。它生成了我的客户端和配置。

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

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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e030f1a0f1d40180f071a050b18070d071b1d2e091c011b1e4040404040400d01401b05" rel="noreferrer noopener nofollow">[email protected]</a>" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>

异常(exception):

{"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)

如果我们查看最里面的异常,它会指出目标主体名称不正确,这意味着 (WCF) 生成了不正确的 UPN。我一定在这里遗漏了一些明显的东西。

最佳答案

我自己想出来了。

我所需要做的就是删除

   <identity>
      <userPrincipalName value="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4429253025376a32252d302f21322d272d31370423362b31346a6a6a6a6a6a272b6a312f" rel="noreferrer noopener nofollow">[email protected]</a>" />
    </identity>

来自Web.config

关于c# - WCF 错误 自动生成的客户端和配置中的目标主体名称不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29407805/

相关文章:

azure - 使用 Azure AD 创建 Azure SQL 数据库登录名/用户

c# - 当它是整数数据类型时,如何在我的 gridview 中找到标签值

c# - 子文件夹应用程序中的 Web api 路由

wcf - Windows 8 Azure 模拟器正在将端口 80 重新映射到 81

.net - 通过 Internet 传输数据的最佳方法(针对 .NET 开发人员)

C# 表达式 - FatalExecutionEngineError

c# - 启用 CLR 仍然不允许我在 sql server 上运行代码

c# - 将视频上传到特定 channel /Youtube

c# - 如何使用 WCF 服务生成 XML 输出?

c++ - 没有运算符 "[]"匹配这些操作数/没有模板代码