[英]Setting credentials when using MetadataExchangeClient
would appreciate some help with this - I feel I must be missing something obvious. 我会感激一些帮助 - 我觉得我必须遗漏一些明显的东西。 I want to use the System.ServiceModel.Description.MetadataExchangeClient to retrieve information about a Web service.
我想使用System.ServiceModel.Description.MetadataExchangeClient来检索有关Web服务的信息。 If the Web service is unsecured, then I have no problem, but if the Web service is a WCF service secured by Windows authentication, then I get an error along the lines of: Metadata contains a reference that cannot be resolved: 'http://server/Service.svc?WSDL'.
如果Web服务不安全,那么我没有问题,但如果Web服务是由Windows身份验证保护的WCF服务,那么我会得到一个错误:元数据包含无法解析的引用:'http:/ /server/Service.svc?WSDL”。
My code is fairly simple: 我的代码很简单:
Uri endpointUri = new Uri("http://server/Service.svc?WSDL");
System.ServiceModel.Description.MetadataExchangeClient mexClient = new System.ServiceModel.Description.MetadataExchangeClient(endpointUri, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);
mexClient.ResolveMetadataReferences = true;
System.Net.NetworkCredential mexNetworkCredentials = new System.Net.NetworkCredential(username, password, domain);
mexClient.HttpCredentials = mexNetworkCredentials;
System.ServiceModel.Description.MetadataSet metadataSet = mexClient.GetMetadata(endpointUri, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);
Am I just missing something in the way that the client needs to specify authentication? 我只是错过了客户端需要指定身份验证的方式吗? I have googled around for a sample code, but nothing seems to match what I have in mind.
我搜索了一个示例代码,但似乎没有任何东西符合我的想法。
Here is the Web.config file (slightly redacted): 这是Web.config文件(稍微编辑):
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<connectionStrings>
<remove name="db"/>
<add name="db" connectionString="server="server";database="database";Integrated Security=SSPI;" providerName="System.Data.SqlClient="/>
</connectionStrings>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation>
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
</system.web>
<location path="." inheritInChildApplications="false">
<system.web>
<authentication mode="Windows"/>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>
</pages>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory"/>
<remove name="ScriptHandlerFactoryAppServices"/>
<remove name="ScriptResource"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</handlers>
</system.webServer>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="SecuredHttpBinding" maxReceivedMessageSize="52428800">
<readerQuotas maxArrayLength="52428800"/>
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="APIServiceBehavior" name="APIService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="SecuredHttpBinding" name="IAPIService" contract="IAPIService" bindingNamespace="http://www.general.com/general/api/v1"/>
</service>
<service behaviorConfiguration="APIServiceBehavior" name="Com.IntApp.Walls.API.InternalAPIService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="SecuredHttpBinding" name="IInternalAPIService" contract="IInternalAPIService" bindingNamespace="http://www.general.com/general/api/v1"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="APIServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</location>
</configuration>
Thanks for any help, Martin 感谢任何帮助,马丁
I will answer my own question in the hopes that this will help someone else later. 我将回答我自己的问题,希望以后可以帮助别人。 I enabled tracing on my client by adding the following block of XML to my App.config file:
我通过在App.config文件中添加以下XML块来启用对客户端的跟踪:
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "D:\log\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
In examining the resulting trace log, I could see that exceptions were being thrown: first because my client had too low a setting for MaxReceivedMessageSize and then because the default settings for MaxNameTableCharCount and MaximumResolvedReferences were too low. 在检查生成的跟踪日志时,我可以看到抛出异常:首先是因为我的客户端对MaxReceivedMessageSize的设置太低,然后因为MaxNameTableCharCount和MaximumResolvedReferences的默认设置太低。 I fixed these with code along the following lines:
我用以下代码修改了这些代码:
BasicHttpBinding basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
basicHttpBinding.MaxReceivedMessageSize = 52428800;
System.Xml.XmlDictionaryReaderQuotas xmlDictionaryReaderQuotas = new XmlDictionaryReaderQuotas();
xmlDictionaryReaderQuotas.MaxNameTableCharCount = 2147483647;
basicHttpBinding.ReaderQuotas = xmlDictionaryReaderQuotas;
System.ServiceModel.Description.MetadataExchangeClient mexClient = new System.ServiceModel.Description.MetadataExchangeClient(basicHttpBinding);
mexClient.MaximumResolvedReferences = 1000;
System.ServiceModel.Description.MetadataSet metadataSet = mexClient.GetMetadata(endpointUri, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);
Hope this saves someone some time! 希望这能节省一些时间!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.