0
votes

Using Exchange EWS managed API 2.0, here is a method which I'm using to validate credentials:

public static bool TestExchangeConnetion(UserSettingDTO credential, ServerSettingDTO serverSetting)
    {
        bool authenticated = false;
        Microsoft.Exchange.WebServices.Data.ExchangeService service;


        service = new ExchangeService((ExchangeVersion)Enum.Parse(typeof(ExchangeVersion), serverSetting.ExchangeVer));
        service.Url = new Uri(serverSetting.ExchangeURL);


        service.Credentials = new NetworkCredential(credential.ExchangeUsername, credential.ExchangePassword);

        try
        {
            //make a call to ensure that credentials are working
            AlternateIdBase response = service.ConvertId(new AlternateId(IdFormat.EwsId, "Placeholder", credential.ExchangeUsername), IdFormat.EwsId);
        }
        // The credentials were authenticated. We expect this exception since we are providing intentional bad data for ConvertId
        catch (ServiceResponseException)
        {
            authenticated = true;
        }

        // The credentials were not authenticated.
        catch (ServiceRequestException)
        {
            authenticated = false;
        }
        catch (Exception)
        {
            authenticated = false;
        }

        return authenticated;
    }

This works absolutely fine to validate credentials but I'm looking for a way to differentiate between invalid credentials and Exchange server downtimes. This code returns false for both. Is it possible to find out if there is an issue communicating to the server (like server downtimes)? The reason for this is I want to notify the user about invalid credentials not server downtimes!

1

1 Answers

3
votes

EWS is just a Web-service that runs on IIS (so IIS is looking after authentication) if you just want to check the credentials why don't you just make a Get request against the services.wsdl file or a Get on the Autodiscover endpoint on the CAS server. If the credentails are incorrect that should return a 401 at that point.

Cheers Glen