ews-managed-api icon indicating copy to clipboard operation
ews-managed-api copied to clipboard

Autodiscovery fail: Call to dnsapi.dll on Linux

Open ststeiger opened this issue 7 years ago • 2 comments

ews-managed-api tries to lookup the _autodiscover._tcp URL from the dns server like nslookup -type=srv _autodiscover._tcp.my_domain.local which is fine, except it makes calls to dnsapi.dll on Linux ! Self-evidently, that fails.

Microsoft.Exchange.WebServices.Dns
internal static class DnsNativeMethods
  {

    [DllImport("dnsapi.dll", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int DnsQuery([In] string pszName, DnsRecordType wType, DnsNativeMethods.DnsQueryOptions options, IntPtr aipServers, ref IntPtr ppQueryResults, int pReserved);

    [DllImport("dnsapi.dll", CharSet = CharSet.Unicode)]
    private static extern void DnsRecordListFree([In] IntPtr ptrRecords, [In] DnsNativeMethods.FreeType freeType);

The solution would be to do this with ArSoft (.NET Core DNS library).

using ARSoft.Tools.Net;
using ARSoft.Tools.Net.Dns;

public static void Test4()
{
    System.Net.NetworkInformation.IPGlobalProperties ipgp = 
        System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();

    // IDnsResolver resolver = new RecursiveDnsResolver(); // Warning: Doesn't work
    IDnsResolver resolver = new DnsStubResolver();

    // List<SrvRecord> srvRecords = resolver.Resolve<SrvRecord>("_ldap._tcp." + ipgp.DomainName, RecordType.Srv);
    List<SrvRecord> srvRecords = resolver.Resolve<SrvRecord>("_autodiscover._tcp." + ipgp.DomainName, RecordType.Srv);


    foreach (SrvRecord thisRecord in srvRecords)
    {
        // System.Console.WriteLine(thisRecord.Name);
        System.Console.WriteLine(thisRecord.Target);
        System.Console.WriteLine(thisRecord.Port);

        // Note: OR LDAPS:// - but Novell doesn't want these parts anyway 
        string url = "LDAP://" + thisRecord.Target + ":" + thisRecord.Port; 
        System.Console.WriteLine(url);
    } // Next thisRecord

}

ststeiger avatar May 16 '18 20:05 ststeiger

Just writing that you can add the (pre-release) nuget-package System.DirectoryServices/4.5.0-rc1 and re-add the file Microsoft.Exchange.WebServices.Data\Autodiscover\DirectoryHelper.cs

And in Microsoft.Exchange.WebServices.Data\Autodiscover\AutodiscoverService.cs modify the function DefaultGetScpUrlsForDomain back to its original state:

      /// <summary>
        /// Defaults the get autodiscover service urls for domain.
        /// </summary>
        /// <param name="domainName">Name of the domain.</param>
        /// <returns></returns>
        private ICollection<string> DefaultGetScpUrlsForDomain(string domainName)
        {
            DirectoryHelper helper = new DirectoryHelper(this);
            return helper.GetAutodiscoverScpUrlsForDomain(domainName);
        }

then it will work (at least on Linux and Windows - tested).

ststeiger avatar May 24 '18 10:05 ststeiger

But it doesn't

Dean-ZhenYao-Wang avatar Oct 21 '19 05:10 Dean-ZhenYao-Wang