2

The following code works fine on windows but on debian is returning a 401 - anonymous request disallowed. It is using DotNet core 2.0.0. If that helps. I think it may be because the machine on windows is somehow discovering the domain while the debian machine is not. I'm not sure. Any help is appreciated.

using System;
using Microsoft.Exchange.WebServices.Data;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeService service = new ExchangeService();
            service.EnableScpLookup = false;
            service.TraceEnabled = true;
            service.UseDefaultCredentials = false;
            service.PreAuthenticate = false;
            service.TraceFlags = TraceFlags.All;
            try
            {
                service.Url = new Uri("https:/domain/ews/Exchange.asmx");
            }
            catch (Exception ex) {
                throw new Exception(string.Format("webService Uri:" + ex));
            }
            try
            {
                service.Credentials = new WebCredentials("username", "pass","domain");
            }
            catch (Exception ex) {
                throw new Exception(string.Format("Credentials:" + ex));
            }
            try
            {
                EmailMessage email = new EmailMessage(service);
                email.ToRecipients.Add("user@domain.com");
                email.Subject = "HelloWorld";
                email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API");
                email.Save();
                email.Send();
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

Edit 1: If it helps I can put the trace of the soap and the exception with email ommited:

<trace Tag="EwsRequestHttpHeaders" Tid="1" Time="2018-07-20 09:31:08Z">
POST /ews/Exchange.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
Accept: text/xml
User-Agent: ExchangeServicesClient/15.00.0913.015
Accept-Encoding: gzip,deflate


</Trace>
<Trace Tag="EwsRequest" Tid="1" Time="2018-07-20 09:31:09Z" 
Version="15.00.0913.015">
 <?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header>
     <t:RequestServerVersion Version="Exchange2013_SP1" />
   </soap:Header>
   <soap:Body>
     <m:CreateItem MessageDisposition="SaveOnly">
       <m:Items>
         <t:Message>
           <t:Subject>HelloWorld</t:Subject>
           <t:Body BodyType="HTML">This is the first email I've sent by using the EWS Managed API</t:Body>
           <t:ToRecipients>
             <t:Mailbox>
               <t:EmailAddress>**********@******.com</t:EmailAddress>
             </t:Mailbox>
           </t:ToRecipients>
         </t:Message>
       </m:Items>
     </m:CreateItem>
   </soap:Body>
 </soap:Envelope>
</Trace>
<Trace Tag="EwsResponseHttpHeaders" Tid="1" Time="2018-07-20 09:31:12Z">
HTTP/1.1 401 Anonymous Request Disallowed
Server: Microsoft-IIS/8.5
request-id: 13c632cd-7642-4e26-ad5d-48dce4b52d35
WWW-Authenticate: Negotiate, NTLM
X-Powered-By: ASP.NET
X-FEServer: FTLPEX02CAS02
Date: Fri, 20 Jul 2018 09:31:11 GMT
Content-Length: 0


</Trace>
Nathan
  • 51
  • 4
  • Does this answer help: [SOAP authentication fails when running a c# app on a linux box](https://stackoverflow.com/q/49121998/33499)? – wimh Jul 20 '18 at 10:30
  • Not really, I tried to cache my credentials and then specify NTML as the auth type then send it on, this works fine on Windows (as does my original code), but has the same error on Debian. It is possible I misunderstood though. – Nathan Jul 20 '18 at 15:40
  • I tried changing the DotNet core version to 2.1 as well but that gave me some weird kerberos error – Nathan Jul 20 '18 at 15:41

1 Answers1

0

This answer is 3 years too late to help the OP, but it may help some other poor head-scratching soul. I was experiencing the same symptoms, but on macOS, with .Net Core 3.1, and the fix I found was to use only the schema + hostname in the service URL. No path elements.

Instead of

service.Url = new Uri("https:/domain/ews/Exchange.asmx");

use

service.Url = new Uri("https:/domain");

I stumbled upon this solution, which I'm sure is glaringly obvious to more experienced .Net devs, after trying all sorts of other potential solutions. I tried including the AD domain in the username (DOMAIN\username), using CredentialCache with an entry for NTLM, toggling PreAuthenticate, including various domain names in NetworkCredential, Basic authorization, and more. All failed with the dreaded 401 until I chopped the path off of the service URL.

Peter
  • 928
  • 10
  • 18