Page 1 of 1

C# Client

Posted: Wed Jul 02, 2014 6:14 am
by proit
Hi there

I have to create a .NET client to OpenEMM. This is my code so far:

Code: Select all

            string username = "MYUSERNAME";
            string password = "MYPASSWORD";
            ChannelFactory<openemm> channelFactory;
            EndpointAddress serviceAddress = new EndpointAddress("http://xx.xx.xx/openemm-ws2/");

            CustomBinding binding = new CustomBinding();
            var security = TransportSecurityBindingElement.CreateUserNameOverTransportBindingElement();
            security.AllowInsecureTransport = true;
            security.IncludeTimestamp = false;
            security.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic256;
            security.MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;

            var encoding = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);

            var transport = new HttpTransportBindingElement();
            transport.MaxReceivedMessageSize = 20000000; // 20 megs

            transport.ProxyAddress = new Uri("http://localhost:8888");
            transport.UseDefaultWebProxy = false;


            channelFactory = new ChannelFactory<openemm>(binding, serviceAddress);

            channelFactory.Endpoint.Behaviors.Add(new CustomCredentials());

            channelFactory.Credentials.UserName.UserName = username;
            channelFactory.Credentials.UserName.Password = password;

            openemm oemmClient = channelFactory.CreateChannel();

            ListMailingsResponse response = oemmClient.ListMailings(new ListMailingsRequest1());
To create the credentials I use the following code:

Code: Select all

    public class CustomCredentials : ClientCredentials
        public CustomCredentials()
        { }

        protected CustomCredentials(CustomCredentials cc)
            : base(cc)


        public override System.IdentityModel.Selectors.SecurityTokenManager CreateSecurityTokenManager()
            return new CustomSecurityTokenManager(this);

        protected override ClientCredentials CloneCore()
            return new CustomCredentials(this);

    internal class CustomSecurityTokenManager : ClientCredentialsSecurityTokenManager
        public CustomSecurityTokenManager(CustomCredentials cred)
            : base(cred)


        public override System.IdentityModel.Selectors.SecurityTokenSerializer CreateSecurityTokenSerializer(System.IdentityModel.Selectors.SecurityTokenVersion version)
            return new CustomTokenSerializer(System.ServiceModel.Security.SecurityVersion.WSSecurity11);

    internal class CustomTokenSerializer : WSSecurityTokenSerializer
        public CustomTokenSerializer(SecurityVersion sv)
            : base(sv)


        protected override void WriteTokenCore(System.Xml.XmlWriter writer, System.IdentityModel.Tokens.SecurityToken token)
            string usn = "MYUSERNAME";
            string pwd = "MYPASSWORD";

            const string tokennamespace = "o";
            DateTime created = DateTime.Now.ToUniversalTime();

            var nonce = getNonce();
            string nonceToSend = Convert.ToBase64String(Encoding.UTF8.GetBytes(nonce));

            string createdStr = created.ToString("yyyy-MM-ddTHH:mm:ssZ");

            //string dec = base64Decode(nonce);
            string passwordToSend = GetSHA1String(nonce + createdStr + pwd);

            System.IdentityModel.Tokens.UserNameSecurityToken unToken = (System.IdentityModel.Tokens.UserNameSecurityToken)token;

                "<{0}:UsernameToken xmlns:wsu=\"\">" +
                "<{0}:Username>" + usn + "</{0}:Username>" +
                "<{0}:Password Type=\"\">" +
                passwordToSend + "</{0}:Password>" +
                "<{0}:Nonce EncodingType=\"\">" +
                nonceToSend + "</{0}:Nonce>" +
                "<wsu:Created>" + createdStr + "</wsu:Created></{0}:UsernameToken>", tokennamespace));

        protected string getNonce()
            string phrase = Guid.NewGuid().ToString();
            return phrase;


        protected string GetSHA1String(string phrase)
            SHA1CryptoServiceProvider sha1Hasher = new SHA1CryptoServiceProvider();
            byte[] hashedDataBytes = sha1Hasher.ComputeHash(Encoding.UTF8.GetBytes(phrase));
            string test = Convert.ToString(hashedDataBytes);
            return Convert.ToBase64String(hashedDataBytes);
(Credit goes to pixus, ... html#p7939)

When I try to call any method (like ListMailings) I get the following error:

Code: Select all

An unhandled exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in mscorlib.dll

Additional information: There was no endpoint listening at that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The inner exception says "The remote server returned an error: (404) Not Found."

The webservice and the username / Password are correct. I tried to call the webservice with the php sample implementation (WSSESoapClient.php and WS_use_example.php). The only difference is that the endpoint URL in php is I also tried this URL without success.

What am I doing wrong? Does anyone have a working .net client?

Best regards
Mike Hachen