2012-01-11 111 views
2

我有一个远程WCF服务,我通过WsHttpBinding的连接。如果我使用空的服务构造函数,这意味着它将从app.config中获取所有配置,一切正常(我的意思是MyService s = new MyService())。 现在我想以编程方式配置wcf。这很简单,直到我到达认证问题,这是很难做到这一点。这里是我使用的app.config,你可以在那里看到我的安全配置。配置WCF服务编程

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
      <binding name="SecuredEndPoint" closeTimeout="00:01:00" openTimeout="00:01:00" 
       receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" 
       transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
       maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
       messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
       allowCookies="false"> 
       <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
        maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
       <reliableSession ordered="true" inactivityTimeout="00:10:00" 
        enabled="true" /> 
       <security mode="Message"> 
        <transport clientCredentialType="Windows" proxyCredentialType="None" 
         realm="" /> 
        <message clientCredentialType="UserName" negotiateServiceCredential="true" 
         algorithmSuite="Default" /> 
       </security> 
      </binding>    
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://MyWcfService.svc" 
      binding="wsHttpBinding" bindingConfiguration="SecuredEndPoint" 
      contract="ServiceReference1.IMyService" name="SecuredEndPoint"> 
      <identity> 
       <certificate encodedValue="*******************************************************************" /> 
      </identity> 
     </endpoint>    
    </client> 
</system.serviceModel> 

回答

11

我已经这样做了,你可能需要修改你的安全模式的代码,你必须在配置

public virtual ChannelFactory<T> Proxy<T>(string address) { 
     //Validate Address 
     if (string.IsNullOrEmpty(address)) throw new ArgumentNullException("Address can not be null or empty."); 
     //Address 
     EndpointAddress endpointAddress = new EndpointAddress(address); 

     //Binding 
     WSHttpBinding wsHttpBinding = new WSHttpBinding(SecurityMode.None, false); 
     wsHttpBinding.OpenTimeout = wsHttpBinding.CloseTimeout = new TimeSpan(0, 1, 0); 
     wsHttpBinding.ReceiveTimeout = wsHttpBinding.SendTimeout = new TimeSpan(0, 10, 0); 
     wsHttpBinding.MaxReceivedMessageSize = wsHttpBinding.MaxBufferPoolSize = 2147483647; 
     wsHttpBinding.BypassProxyOnLocal = wsHttpBinding.AllowCookies = wsHttpBinding.TransactionFlow = false; 
     wsHttpBinding.MessageEncoding = WSMessageEncoding.Text; 
     wsHttpBinding.TextEncoding = Encoding.UTF8; 
     wsHttpBinding.UseDefaultWebProxy = true; 
     wsHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; 
     wsHttpBinding.ReaderQuotas = new XmlDictionaryReaderQuotas(); //ReaderQuotas, setting to Max 
     wsHttpBinding.ReaderQuotas.MaxArrayLength = wsHttpBinding.ReaderQuotas.MaxBytesPerRead = 2147483647; 
     wsHttpBinding.ReaderQuotas.MaxStringContentLength = wsHttpBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647; 
     wsHttpBinding.ReaderQuotas.MaxDepth = 2147483647; 

     //Create the Proxy 
     ChannelFactory<T> proxy = new ChannelFactory<T>(wsHttpBinding, endpointAddress); 

     //Sets the MaxItemsInObjectGraph, so that client can receive large objects 
     foreach (var operation in proxy.Endpoint.Contract.Operations) { 
      DataContractSerializerOperationBehavior operationBehavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>(); 
      //If DataContractSerializerOperationBehavior is not present in the Behavior, then add 
      if (operationBehavior == null) { 
       operationBehavior = new DataContractSerializerOperationBehavior(operation); 
       operation.Behaviors.Add(operationBehavior); 
      } 
      //IMPORTANT: As 'operationBehavior' is a reference, changing anything here will automatically update the value in list, so no need to add this behavior to behaviorlist 
      operationBehavior.MaxItemsInObjectGraph = 2147483647; 
     } 
     return proxy; 
} 

在此proxy对象,你需要做.CreateChannel()使用它。

希望这会有所帮助。

+0

但在那里我可以看到在身份代码证书encodedValue问题,这真的是我的大问题。 – Wasim 2012-01-11 19:33:08

+0

试试这个'EndpointIdentity身份= EndpointIdentity.CreateX509CertificateIdentity(新System.Security.Cryptography.X509Certificates.X509Certificate2(Encoding.UTF8.GetBytes( “您的证书”));',把给这个身份'EndpointAddress'构造你都会有。要修改安全模式 – 2012-01-12 04:39:08

+0

我:客户端无法确定基于目标地址http身份服务主体名称://MyService.svc/securedendpoint”的SspiNegotiation/Kerberos的宗旨目标地址的标识必须是UPN我的代码设置识别码是:var encodedValue = “**** Y”; EndpointIdentity identity = EndpointIdentity.CreateX509CertificateIdentity(new System。 Security.Cryptography.X509Certificates.X509Certificate2(Encoding.UTF8.GetBytes(encodedValue))); – Wasim 2012-01-15 07:48:17