2012-01-11 15 views
2

私はリモートwcfサービスを持っています。WSHttpBindingで接続します。空のサービスコンストラクタを使用すると、app.configからすべての設定を取得することになりますが、すべてがOKです(MyService = 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

、あなたはあなたがそれを使用する.CreateChannel()を行う必要があります。このproxyオブジェクトに設定

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; 
} 

で持っているセキュリティモードのためのコードを変更する必要がある場合があります。

これが役に立ちます。

+0

しかし、IDコードでencodedValueという証明書の問題を見ることができますが、これは本当に大きな問題でした。 – Wasim

+0

このEndpointIdentity identity = EndpointIdentity.CreateX509CertificateIdentity(新しいSystem.Security.Cryptography.X509Certificates.X509Certificate2(Encoding.UTF8.GetBytes( "Your Certificate"));を実行し、このIDを 'EndpointAddress'コンストラクタに渡します。セキュリティモードを変更する –

+0

SspiNegotiation/Kerberosの目的のためにクライアントがターゲットアドレスhttp://MyService.svc/securedendpointのIDに基づいてサービスプリンシパル名を判別できませんターゲットアドレスのIDはUPNでなければなりませんidenetiyを設定するコードは次のとおりです:var encodedValue = "**** Y"; EndpointIdentity identity = EndpointIdentity.CreateX509CertificateIdentity(新しいシステム。 Security.Cryptography.X509Certificates.X509Certificate2(Encoding.UTF8.GetBytes(encodedValue))); – Wasim

関連する問題