none
EWS Error: The request failed. The remote server returned an error: (401) Unauthorized. while accessing Root PF RRS feed

  • Question

  • Hi,

    I am trying to add Calendare public folder with EWS, but getting this error while accessing Root PF:

    The request failed. The remote server returned an error: (401) Unauthorized.

    My source code is here:

    string szEwsUrl = https://ex2010sp2.adv.local/ews/exchange.asmx";
    ExchangeService exchangeService= new ExchangeService(ExchangeVersion.Exchange2010_SP1);
    System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateRemoteCertificate);
    exchangeService.Timeout = 30 * 60 * 1000;WebCredentials wc = new WebCredentials(System.Net.CredentialCache.DefaultCredentials);
    exchangeService.Credentials = wc;
    exchangeService.UseDefaultCredentials = true;
    exchangeService.Url = new Uri(szEwsUrl);
    //This line causing the Unauthorized exception
    Folder publicFoldersRoot = Folder.Bind(exchangeService, WellKnownFolderName.PublicFoldersRoot);
    FolderId pfRootId = publicFoldersRoot.Id;
    .................

    I am using Defautl Authentication, and running the source code under builtin administrator credentials.

    Interestingly, the same source code is running fine on my test exchange 2010 servers, while failing on 2 of my production servers.

    Thanks and Regards,


    Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com

    Tuesday, December 11, 2012 12:09 PM

Answers

  • Hi,

    It turned out to be EWS Url issue.

    On production server, we were using externalUrl (https://mail.company.com/ews/exchange.asmx) of Exchange Web Service virtual directory. When we tried to browse that url in internet explorer, then it directed to OWA, instead of showing wsdl contents. Once we used InternalUrl (which containing internal FQDN of CAS server) then issue resolved.

    Also, we saw that, the user, whose credentials is to be used in EWS connection, should have the permission on public folders, otherwise the public folders wont be visible to the user via EWS.

    Thanks and regards


    Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com

    • Marked as answer by Laeeq Qazi Thursday, December 13, 2012 2:21 PM
    Thursday, December 13, 2012 2:15 PM

All replies

  • The Builtin-Administrator on a Server that isn't a Domain controller would be a local account not a domain account. Meaning it would have no rights (nor would you be able to assign it any) within in Exchange so 401 would be an appropriate response.

    Why it works on your test server is probably because that server is a Domain Controller with all the roles installed hence you Builtin-Administrator has a domain context.

    Cheers
    Glen

    Wednesday, December 12, 2012 5:08 AM
  • Hi Glen,

    Thanks for your reply. By built in administrator, I meant that I used builtin AD administrator account. I use Domain\Administrator credentials to log into exchange server, where I am trying to create calendar pf using above code. I am also able to manage exchange using same account via EMS\EMC.

    Thanks and Regards,


    Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com

    Wednesday, December 12, 2012 8:57 AM
  • To access anything in EWS requires you will need to have client permissions to the Folder your accessing which isn't the same as accessing objects via the EMS or EMC. The first step would be to check the client permissions on the public folder root and ensure that user has access.

    You could also try using the EWSEditor http://ewseditor.codeplex.com to test it outside of your code. Eg use the "Other - Open folder by Id" menu item

    Cheers
    Glen


    Thursday, December 13, 2012 5:43 AM
  • Hi,

    It turned out to be EWS Url issue.

    On production server, we were using externalUrl (https://mail.company.com/ews/exchange.asmx) of Exchange Web Service virtual directory. When we tried to browse that url in internet explorer, then it directed to OWA, instead of showing wsdl contents. Once we used InternalUrl (which containing internal FQDN of CAS server) then issue resolved.

    Also, we saw that, the user, whose credentials is to be used in EWS connection, should have the permission on public folders, otherwise the public folders wont be visible to the user via EWS.

    Thanks and regards


    Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com

    • Marked as answer by Laeeq Qazi Thursday, December 13, 2012 2:21 PM
    Thursday, December 13, 2012 2:15 PM