Connecting to RETS Servers with UserAgent Requirement

531 Views Asked by At

I am hoping there is someone here who is familiar with a Real Estate data standard known as RETS. The National Association of Realtors provides a dll for interfacing with their services called libRETS, but it is not being supported like it once was and recent events have prompted us to create our own as a replacement. For logistics reasons, we can't do this in Core and are using the current C#.Net 4.7.2.

There are 2 or 3 different "security levels" for connecting to a RETS Server, with the method being a per case basis from one MLS to the next. We can successfully connect to those who only require a login and password, but are hitting a wall on those who also require what is called a UserAgent and UserAgentPassword, which must passed somehow using Md5 encryption. The server is returning:

The remote server returned an error: (401) Unauthorized.

 private WebResponse GetLoginBasicResponse()//*** THIS ONE WORKS ***
    {
        try
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            var request = (HttpWebRequest)WebRequest.Create(new Uri(_cred.loginUri));
            request.Method = "GET";
            request.Headers.Add("RETS-Version", _retsVersion);
            request.Credentials = new NetworkCredential(_login, _password);
            return request.GetResponse();
        }
        catch (Exception ex)
        {
            string ignore = ex.Message;
            return null;
        }
    }
    private WebResponse GetLoginWithUserAgentResponse()//*** THIS ONE DOES NOT WORK ***
    {
        try
        {
           // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            var request = (HttpWebRequest)WebRequest.Create(new Uri(_cred.loginUri));
            request.Method = "GET";
            request.Headers.Add("RETS-Version", _retsVersion);

            if (!string.IsNullOrEmpty(_cred.userAgent))
            {
                request.UserAgent = Md5(_cred.userAgent + ":" + _cred.userAgentPassword);
                //request.Headers.Add("RETS-UA-Authorization", "Digest " + Md5(_cred.userAgent + ":" + _cred.userAgentPassword));
            }
            request.Credentials = new NetworkCredential(_login, _password);
            return request.GetResponse();
        }
        catch (Exception ex)
        {
            string ignore = ex.Message;
            return null;
        }
    }
    public string Md5(string input) //*** Borrowed this from from .NET Core Project and presume it works
    {
        // Use input string to calculate MD5 hash
        using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
        {
            byte[] inputBytes = Encoding.ASCII.GetBytes(input);
            byte[] hashBytes = md5.ComputeHash(inputBytes);

            // Convert the byte array to hexadecimal string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("X2"));
            }
            return sb.ToString();
        }
    }
3

There are 3 best solutions below

0
On BEST ANSWER

We were not able to solve the issue in .NET but found a .NET Core project in GitHub that we are using instead. https://github.com/CrestApps/RetsConnector

This case can be closed

2
On

Page 20 of this document describes how to build the UA header: https://www.ranww.org/documents/resources/rets_1_8.pdf

There’s a few other fields you need to include.

2
On

Not seeing an option to "Mark as Answer". Have tried both MS Edge and Google Chrome