使用EWS托管API验证登录凭据

时间:2016-04-13 03:20:41

标签: c# exchangewebservices office365api

是否有办法验证用户使用EWS托管API提供的登录凭据而不发送电子邮件。我在[ExchangeService]对象上使用AutodiscoverUrl方法但是它需要太长时间?任何建议都会很感激。

        try
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
            service.Credentials = new WebCredentials("xxxx@yyy.com", "password");
            service.AutodiscoverUrl("xxxx@yyy.com", RedirectionUrlValidationCallback);    

            Console.WriteLine("Credentials validated successfully");
            Console.ReadLine();

        }

        catch(Exception e)
        {
            Console.WriteLine(e.Message);
            Console.ReadLine();
        }

1 个答案:

答案 0 :(得分:5)

提高速度的一种方法,不是很好但是有效,就是自己设置服务网址。 首先通过在调试模式下运行autodiscoverurl方法来获取交换服务URL:

enter image description here

然后,不是在服务上运行autodiscoverurl方法,而是添加从autodiscoverurl方法获得的url:

 service.Url = new Uri("https://yourExchangeURL.com/EWS/Exchange.asmx");

您可以在邮件帐户(Check exchange credentials remotely and check user logged in

上发出请求,而不是调用自动发现来验证用户凭据
var findFolderResults = service.FindFolders(WellKnownFolderName.Root, new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0), new FolderView(10));

您的最终代码将如下所示

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new WebCredentials("xxxx@yyy.com", "password");
service.Url = new Uri("https://yourExchangeURL.com/EWS/Exchange.asmx");
var findFolderResults = service.FindFolders(WellKnownFolderName.Root, new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0), new FolderView(10));