3

I am trying to understand how I should configure a web site (ASP.Net) that need to pass the user credential and get some mail things

I get different kind of errors depending on the app pool configure, What is the complete way to do it? I mean code + Configuration ;o)

I don't want to write the user and password but get them through the windows authentication

Thanks

code:

Service = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
            {
                Url =
                    new Uri(
                    "https://xxx/yyy.asmx"),
                //UseDefaultCredentials = true doesnt help
            };
        var view = new ItemView(1820);

        // Find the first email message in the Inbox that has attachments. This results
        FindItemsResults<Item> results = Service.FindItems(folderName, view); 
        //Here I get the error 401
Leok
  • 31
  • 1
  • 3

1 Answers1

4

Take a look at Get started with EWS Managed API client applications, it covers the basics to get you up and rolling. Note that when you use UseDefaultCredentials, you have to be on a domain joined machine, with the user logged in, and you must be targeting an on-prem server. We tested that scenario out and the following code works in that scenario.

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

namespace HelloWorld
{
    class Program
    {
        static void Main()
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
            service.UseDefaultCredentials = true;
            service.TraceEnabled = true;
            service.TraceFlags = TraceFlags.All;
            //Replace the URL below with your own, but using AutoDiscover is recommended instead
            service.Url = new Uri("https://computer.domain.contoso.com/EWS/Exchange.asmx");

            FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox,
                                                               new ItemView(1));
        }
    }
}

If that doesn't work for you, then you might want to try using impersonation. See this thread, where I think they've got a similar situation . More information about impersonation is on MSDN: Impersonation and EWS in Exchange.

Mimi Gentz
  • 1,568
  • 8
  • 13
  • thank you for the answer but is still not clear, my scenario is pretty simple and maybe is a double-hoop issue. The IIS application get the request from an authenticated Windows integrated AD user, so it recognize the user credentials. Still I don't know how to pass them implicitly to EWS... The client is logged in, but the FindItemsResults fails with 401 and similar errors – Leok Jul 13 '14 at 10:26
  • Thanks for mentioning that UseDefaultCredentials only works for an on-premises Exchange Server. (I don't recall seeing that mentioned in Microsoft's documentation) – RenniePet Sep 23 '17 at 02:35