When using manually set credentials, EWS auto disc

2019-05-14 22:54发布

问题:

I am running into a very strange issue and I am convinced it is just something stupid that I am overlooking. Using the EWS managed API, I try to connect to a mailbox to read the contacts. Originally, I used the default credentials, in which case the auto discovery worked. The problem is that later on we want to run this on a server and impersonate a user, so I changed it by manually specifying the credentials. This then broke, even when using my own credentials.

As an example, this worked:

service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.AutodiscoverUrl("user@example.com", redirect => true);

This did not:

service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.UseDefaultCredentials = false;
service.Credentials = new NetworkCredential("user", "pass", "EXAMPLE_DOMAIN");
service.AutodiscoverUrl("user@example.com", redirect => true);

The given network credentials should be exactly the same as the default credentials, but when enabling the tracing, I get the response "401 Unauthorized" in the second case, so it is not acting as if it is the same.

What am I missing?

回答1:

Ok, it seems that you need to use WebCredential("user", "pass", "EXAMPLE_DOMAIN"). I am not exactly sure why WebCredentials work but NetworkCredentials does not.