如何使用EWS托管API验证Exchange凭据并验证Exchange服务器通信?

时间:2016-04-27 02:54:32

标签: c# exchangewebservices ews-managed-api

使用Exchange EWS托管API 2.0,这是我用来验证凭据的方法:

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;
    }

这可以很好地验证凭据,但我正在寻找一种方法来区分无效凭据和Exchange服务器停机时间。此代码对两者都返回false。 是否有可能发现是否存在与服务器通信的问题(如服务器停机时间)?原因是我想通知用户有关无效凭据而不是服务器停机时间的信息!

1 个答案:

答案 0 :(得分:3)

EWS只是一个在IIS上运行的Web服务(因此IIS正在考虑身份验证),如果您只是想检查凭据,为什么不对services.wsdl文件发出Get请求或者获取CAS服务器上的自动发现端点。如果凭证不正确,则应在该点返回401。

干杯 格伦