我正在尝试使用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);
        }

    }

最佳答案

尝试提供另一个具有权限的登录名或凭据。如果您处于AD环境中,请不要忘记指定域:

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

关于c# - Sharepoint 2013 EWS收件箱WebPart错误401未经授权,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19091521/

10-10 05:04