C# Client

Use this forum for all questions related to the source code of OpenEMM

Moderator: moderator

C# Client

Postby proit » Wed Jul 02, 2014 6:14 am

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

            // DECOMMENTARE LE SEGUENTI 2 RIGHE PER PASSARE PER FIDDLER
            transport.ProxyAddress = new Uri("http://localhost:8888");
            transport.UseDefaultWebProxy = false;

            binding.Elements.Add(security);
            binding.Elements.Add(encoding);
            binding.Elements.Add(transport);

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

            channelFactory.Endpoint.Behaviors.Remove<ClientCredentials>();
            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;

            writer.WriteRaw(String.Format(
                "<{0}:UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">" +
                "<{0}:Username>" + usn + "</{0}:Username>" +
                "<{0}:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">" +
                passwordToSend + "</{0}:Password>" +
                "<{0}:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">" +
                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, http://forum.openemm.org/using-openemm-f3/topic2449.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 http://xx.xxx.xx/openemm-ws2/ 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 http://xx.xxx.xx/openemm-ws2/emmservices.wsdl. I also tried this URL without success.

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

Best regards
Mike Hachen
proit
 
Posts: 1
Joined: Wed Jul 02, 2014 5:42 am

Return to Development

Who is online

Users browsing this forum: No registered users and 2 guests

cron