Exchange Server TechCenter > Exchange Server Forums > Development > Got "The remote server returned an error: (401) Unauthorized" error when using EWS managed api 1.0
Ask a questionAsk a question
 

QuestionGot "The remote server returned an error: (401) Unauthorized" error when using EWS managed api 1.0

  • Tuesday, November 03, 2009 8:05 PMzhengliWan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am trying to write a custom SharePoint web part to get user's appointment and email inbox from our company's Exchange server. I use defalut credential (supposed to be the logged in user's credential) to find inbox mailmessage items by passing logged in user's smtp address. The web part works fine on the box that hosts the web part. Different users log into that box and bring up the SharePoint site, they will see their corresponding mail and appointment. However, if we try to bring up that SharePoint site outside of that box, we got "The remote server returned an error: (401) Unauthorized" error. Based on the error trace, the error happened on the function call on Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(...). The following is my code: public IEnumerable GetEmails(string userId, DateTime mailDate, int count) { string url = userId.Trim() + "@mycompany.com"; ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); service.PreAuthenticate = true; service.Credentials = new WebCredentials(System.Net.CredentialCache.DefaultCredentials); service.Url = new Uri(@"https://mail.mycompany.com/EWS/Exchange.asmx"); //Exchange Stores Date and Times as UTC so try converting it to UTC SearchFilter receiveStart = new SearchFilter.IsGreaterThanOrEqualTo(EmailMessageSchema.DateTimeReceived, mailDate.ToUniversalTime()); SearchFilter receiveEnd = new SearchFilter.IsLessThan(EmailMessageSchema.DateTimeReceived, mailDate.AddDays(1).ToUniversalTime()); SearchFilter.SearchFilterCollection filterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.And); filterCollection.Add(receiveStart); filterCollection.Add(receiveEnd); ItemView itemView = new ItemView(count); itemView.PropertySet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.Subject, EmailMessageSchema.Sender); FindItemsResults findResults; findResults = service.FindItems(new FolderId(WellKnownFolderName.Inbox, new Mailbox(url)), filterCollection, itemView); List list = new List(); foreach (Item item in findResults.Items) { if (item is EmailMessage) { list.Add((EmailMessage)item); } } return list; } The error stack trace is: The remote server returned an error: (401) Unauthorized. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Net.WebException: The remote server returned an error: (401) Unauthorized. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [WebException: The remote server returned an error: (401) Unauthorized.] System.Net.HttpWebRequest.GetResponse() +1126 Microsoft.Exchange.WebServices.Data.ServiceRequestBase.Emit() +755 Microsoft.Exchange.WebServices.Data.ServiceRequestBase.InternalExecute() +170 [ServiceRequestException: Request failed. The remote server returned an error: (401) Unauthorized.] Microsoft.Exchange.WebServices.Data.ServiceRequestBase.InternalExecute() +533 Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute() +64 Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(IEnumerable`1 parentFolderIds, SearchFilter searchFilter, String queryString, ViewBase view, Grouping groupBy, ServiceErrorHandling errorHandlingMode) +505 Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(FolderId parentFolderId, SearchFilter searchFilter, ItemView view) +219 FCS.SharePoint.FCSWPMail.ExchangeEmail.GetEmails(String userId, DateTime mailDate, Int32 count) +1526 FCS.SharePoint.FCSWPMail.FCSWPMail.Render(HtmlTextWriter writer) +509 Microsoft.SharePoint.WebPartPages.SPChrome.RenderPartContents(HtmlTextWriter output, WebPart part) +80 [WebPartException: Request failed. The remote server returned an error: (401) Unauthorized.] Microsoft.SharePoint.WebPartPages.SPChrome.RenderPartContents(HtmlTextWriter output, WebPart part) +238 Microsoft.SharePoint.WebPartPages.SPChrome.RenderWebPart(HtmlTextWriter output, WebPart part) +100 Microsoft.SharePoint.WebPartPages.WebPartZone.RenderZoneCell(HtmlTextWriter output, Boolean bMoreParts, WebPart part) +254 Microsoft.SharePoint.WebPartPages.WebPartZone.RenderWebParts(HtmlTextWriter output, ArrayList webParts) +771 Microsoft.SharePoint.WebPartPages.WebPartZone.Render(HtmlTextWriter output) +1449 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +42 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +253 System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +87 System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +53 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +42 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 System.Web.UI.Page.Render(HtmlTextWriter writer) +38 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4240 -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:2.0.50727.4200; ASP.NET Version:2.0.50727.4016 Any help will be greatly appreciated. Thanks

All Replies

  • Tuesday, November 03, 2009 8:22 PMHenning KrauseMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    if I understand you correctly, you have one Exchange box and one different SharePoint box.

    Assuming, this is correct, you are running into a double-hop problem. NTLM, the default authentication method, cannot be delegated to another server. You need to configure Kerberos for this to work.

    Kind regards,
    Henning
  • Tuesday, November 03, 2009 8:29 PMzhengliWan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Henning, You are right. I have two boxes. One is for SharePoint and our company's Exchange server is on another box. However, I do believe I am using Kerberos on my SharePoint box. It puzzles me that it is OK when I log into SharePoint box and bring up the site but it is not OK if I bring up the same SharePoint site outside of that SharePoint box. I debugged into it and found the identity of HttpContext.Current are the same under the above two situations. I just cannot debug into Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(...) function to find out what credentials the server was using under the two different situation. Can you shed some lights on this? Thanks a lot for your quick response.