Sharepoint 2013 EWS inbox webpart错误401未经授权

时间:2013-09-30 10:08:52

标签: c# sharepoint sharepoint-2013 exchangewebservices

我正在尝试使用EWS访问收件箱邮件。 Kerberos已配置。但是当我在我的webpart中使用service.UseDefaultCredentials = true时,我得到401错误(未经授权)。什么是解决此问题的解决方案。

   private static bool ValidateCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors) {
        return true;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {             

            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);

            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateCertificate);

            service.UseDefaultCredentials = true;

            service.Url = new Uri(EWSuri.uri);


            SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
                                                                      new SearchFilter.IsEqualTo(
                                                                       EmailMessageSchema.IsRead, false), new SearchFilter.IsGreaterThan(ItemSchema.DateTimeReceived, DateTime.Now.AddDays(-14)));

            FindItemsResults<Item> findResults = service.FindItems(
                WellKnownFolderName.Inbox, sf,
                new ItemView(int.MaxValue));

            emailblock.InnerHtml = "<div data-itemscount=\"" + findResults.Items.Count + "\"></div>";

            foreach (Item item in findResults.Items)
            {
                EmailMessage mes = (EmailMessage)item;

                emailblock.InnerHtml += "<div class=\"emailitem\"><p class=\"emailline clearfix\"><span class=\"emailfield\">From:</span>" +
                            "<span class=\"emailtext\">" + " " + mes.Sender.Name + "</span></p>" +
                            "<p class=\"emailline clearfix\"><span class=\"emailfield\">Subject:</span>" +
                            "<span class=\"emailtext\">" + " " + item.Subject + "</span></p>" +
                            "<p class=\"emailline clearfix\"><span class=\"emailfield\">Received:</span>" +
                            "<span class=\"emailtext\">" + " " + item.DateTimeSent + "</span></p></div>";

            }
        }

        catch (Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverRemoteException ex)
        {
            Console.WriteLine("Exception thrown:{0}" ,ex.Message);
        }

    }  

2 个答案:

答案 0 :(得分:0)

尝试提供其他具有权限的登录信息或凭证。如果您在AD环境中,请不要忘记指定域名:

exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
exchangeService.Credentials = new NetworkCredential(ExchangeConfig.user,
                                                            ExchangeConfig.password,
                                                            ExchangeConfig.domain);
exchangeService.AutodiscoverUrl(ExchangeConfig.usermail);

答案 1 :(得分:0)

service.UseDefaultCredentials = true;当您使用模拟使用自己的计算机登录时,这将无效。要解决此问题,您需要获得对所有邮件帐户具有读取权限的管理员用户,然后传递admin用户的显式凭据,然后使用EWS的模拟类,并解决问题