Win32Exception:使用WebClient调用WCF服务时,目标主体名称不正确 [英] Win32Exception:The target principal name is incorrect when calling a WCF service with WebClient

查看:103
本文介绍了Win32Exception:使用WebClient调用WCF服务时,目标主体名称不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将大量示例代码与linqpad放在一起,显示了WCF Web服务调用,而没有从WSDL中创建代理类.这是我所拥有的:

I put together a quick chunk of example code with linqpad showing a WCF webservice call without creating a proxy class from the WSDL. Here's what I have:

using (var wb = new WebClient())
{
  wb.Credentials = CredentialCache.DefaultNetworkCredentials;
  wb.Headers.Add("Content-Type: text/xml;charset=UTF-8");
  wb.Headers.Add("SOAPAction: \"http://tempuri.org/Core/Project_GetNumberForExternalUse\"");

  String requestString = @"
    <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">
    <soapenv:Header/>
    <soapenv:Body>
        <tem:Project_GetNumberForExternalUse>
          <!--Optional:-->
          <tem:extData1>ext 1</tem:extData1>
          <!--Optional:-->
          <tem:extData2>ext 2</tem:extData2>
          <!--Optional:-->
          <tem:extData3>ext 3</tem:extData3>
          <!--Optional:-->
          <tem:extData4>ext 4</tem:extData4>
          <!--Optional:-->
          <tem:extDataLong>ext 1ext 1ext 1ext 1ext 1</tem:extDataLong>
        </tem:Project_GetNumberForExternalUse>
    </soapenv:Body>
  </soapenv:Envelope>
  ";

  var response = wb.UploadString("http://myserver:55002/Core.svc", "POST", requestString);
  response.Dump();
}

当连接到localhost而不是myserver时,此方法工作正常. WCF服务被配置为使用Windows身份验证.基于错误,我猜测问题是授权中未包含服务主体名称.如果我将WCF客户端与代理一起使用,请在终结点/身份"配置部分中解决此问题.但是,我没有看到在代码中设置此方法的方法.有没有办法做到这一点?还有其他方法可以解决此要求吗?

This works fine when connecting to localhost but not myserver. The WCF service is configured to use windows authentication. Based on the error I'm guessing the issue is that the service principal name is not being included in the authorization. If I was using a WCF client with a proxy I'd address this with the endpoint/identity configuration section. However, I'm not seeing a way to set this in code. Is there not a way to do so?. Is there some other way to get around this requirement?

推荐答案

尝试使用AuthenticationManager.CustomTargetNameDictionary指定在通过WebClient调用该URL时要使用的SPN:

Try using AuthenticationManager.CustomTargetNameDictionary to specify the SPN you want to use when invoking that URL with WebClient:

http://msdn.microsoft.com/zh-cn/library/system.net.authenticationmanager.customtargetnamedictionary.aspx

这篇关于Win32Exception:使用WebClient调用WCF服务时,目标主体名称不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆