2

I have a remote wcf service, I connect it by WSHttpBinding. If I use the empty service constructor which mean it will take all the configurations from the app.config , everything is ok, (I mean MyService s = new MyService()). Now I want to configure the wcf programmatically . it's simple till I arrive to the authentication issue , it was so hard to do that . Here is the app.config which I use , you can see there my security configurations .

<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>
0

1 Answer 1

11

I have done this, you might have to modify your code for security mode you have in config

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

On this proxy object you will need to do .CreateChannel() to use it.

Hope this helps.

6
  • But where I can see the certificate encodedValue issue in the identity code , this was really my big problem .
    – Wasim
    Jan 11, 2012 at 19:33
  • Try this EndpointIdentity identity = EndpointIdentity.CreateX509CertificateIdentity(new System.Security.Cryptography.X509Certificates.X509Certificate2(Encoding.UTF8.GetBytes("Your Certificate")); and put give this identity to EndpointAddress constructor. You will have to modify Security Mode. Jan 12, 2012 at 4:39
  • I got : Client cannot determine the Service Principal Name based on the identity in the target address MyService.svc/securedendpoint' for the purpose of SspiNegotiation/Kerberos. The target address identity must be a UPN identity (like acmedomain\alice) or SPN identity (like host/bobs-machine). My code to set the idenetiy is : var encodedValue = "****Y"; EndpointIdentity identity = EndpointIdentity.CreateX509CertificateIdentity(new System.Security.Cryptography.X509Certificates.X509Certificate2(Encoding.UTF8.GetBytes(encodedValue)));
    – Wasim
    Jan 15, 2012 at 7:48
  • did you know, that there's eg TimeSpan.FromSeconds(int seconds) - instead of doing sth like new TimeSpan(0, 0, 0, int seconds) :)
    – user57508
    Feb 1, 2012 at 10:12
  • 1
    Yes. This had written this code almost 2 yrs back. Are you crawling my post ;) I guess I need to start refactoring my code :) Feb 1, 2012 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.