Using webservices in .Net

Use this forum for questions regarding adoption and functionality of OpenEMM

Moderator: moderator

mhooper
Posts: 2
Joined: Wed Feb 27, 2013 5:09 pm

Using webservices in .Net

Post by mhooper »

Hi all,

I am trying to get my head around the web services

Here is the binding from my web.config file:

Code: Select all

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="openemmSoap11">
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="Digest" />
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://*******************/openemm-ws2/" binding="basicHttpBinding"
        bindingConfiguration="openemmSoap11" contract="emmservice.openemm"
        name="openemmSoap11" />
    </client>
  </system.serviceModel>
And here is my implementation:

Code: Select all

var param = new emmservice.MapItem[]
                {
                    new MapItem {key = "email", value = "********@************.co.uk"},
                    new MapItem {key = "mailtype", value = "0"}, new MapItem {key = "gender", value = "0"}
                };
            
            var service = new emmservice.AddSubscriberRequest
                {
                    parameters = param,
                    overwrite = false,
                    doubleCheck = true,
                    keyColumn = "email"
                };

            var request = new openemmClient();
            request.ClientCredentials.UserName.UserName = "*************";
            request.ClientCredentials.UserName.Password = "************";
            request.Open();
            request.AddSubscriber(service);
            request.Close();
There error message I am getting is:

com.sun.xml.wss.XWSSecurityException: Receiver Requirement for Digested Password has not been met; nested exception is com.sun.xml.wss.XWSSecurityException: com.sun.xml.wss.XWSSecurityException: Receiver Requirement for Digested Password has not been met

As you can see from my bindings, I am using the Digest authentication. Anyone with any ideas?

Thanks
mhooper
Posts: 2
Joined: Wed Feb 27, 2013 5:09 pm

Re: Using webservices in .Net

Post by mhooper »

OK, something tells me not to be so lazy! Having looked at the PHP sample code, it looks like I have to change the password in code. Here is what I have so far:

To get the connecting computer name I use :

Code: Select all

        private static string GetFqdn()
        {
            const string domainName = "***********";
            var hostName = Dns.GetHostName();
            var fqdn = "";
            if (!hostName.Contains(domainName))
                fqdn = hostName + "." + domainName;
            else
                fqdn = hostName;

            return fqdn;
        }
To get the nonce I use :

Code: Select all

        private static string GetNonce()
        {
            var randomGuid = Guid.NewGuid();
            var md5 = MD5.Create();
            var data = Encoding.ASCII.GetBytes(randomGuid + "_" + GetFqdn());

            var result = md5.ComputeHash(data);

            return Convert.ToBase64String(result);
        }
And to convert the password to Base64 I use

Code: Select all

        public string GetPasswordDigestAsBase64(string password)
        {
            var utcDatestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
            var sha1 = SHA1.Create();
            return Convert.ToBase64String(sha1.ComputeHash(Encoding.UTF8.GetBytes(GetNonce() + utcDatestamp + password)));
        }
Any ideas?
pixus
Posts: 10
Joined: Tue Dec 04, 2012 4:53 pm

Re: Using webservices in .Net

Post by pixus »

I got it.
Maybe your error is in using ASCII ?
this is my solution.

Code: Select all

string usn = "MyUsername";
            string pwd = "MyPassword";

            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 passwordToSend = GetSHA1String(nonce + createdStr + pwd);
with the following functions

Code: Select all

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);
        }
Post Reply