C# EWS Unable to browse current users folders RRS feed

  • Question

  • Hi, 

    I'm hoping you guys can help me, I'm trying to alter one of the code examples from the Exchange Code Examples.

    The one I'm trying to alter is the "Creating Weekly Recurring Appointment" but I'm having an issue. I'm trying to change it so that it uses the current logged on user credentials instead of having to type them in via the console window. The problem I'm having is that when I come to query the server I'm not getting that users Calender, and I suspect that users folders in general.

    This is how I'm getting the current users credentials:

    Uri uri = new Uri("");
    ICredentials credentials = CredentialCache.DefaultCredentials;   
    NetworkCredential credential = credentials.GetCredential(uri, "Basic");

    This is the change to the auto discover code:

    if (userData.AutodiscoverUrl == null)
         Console.Write(string.Format("Using Autodiscover to find EWS URL for {0}. Please wait... ", userData.EmailAddress));
          service.AutodiscoverUrl(System.DirectoryServices.AccountManagement.UserPrincipal.Current.EmailAddress, RedirectionUrlValidationCallback);
         userData.AutodiscoverUrl = service.Url;
         Console.WriteLine("Autodiscover Complete");

    However, when I run this code:

    var a = service.FindFolders(WellKnownFolderName.Inbox, new FolderView(10));

    it is returning the folders from a different users inbox. 

    Why is this?

    Other symptoms: When I run the default "CreateWeeklyRecurringAppointment" method from the example with my changes in place the appointment is created in the other users calender and not the current logged in user.

    If you can provide any insight it would be hugely appreciated.



    Thursday, August 6, 2015 4:37 PM

All replies

  • (Apologies for the poor link entry, as my account wasn't verified it wouldn't let me put a link in properly)
    Thursday, August 6, 2015 4:38 PM
  • All Autodiscover returns is the correct CAS server to use for EWS, it doesn't tell EWS what mailbox you want to access by default the Mailbox that will be accessed via the code your using is that of the credentials your using. If you want to access a Mailbox other those of this credentials you need to use the FolderId overload like

    FolderId InboxId = new FolderId(WellKnownFolderName.Inbox,"");
    var a = service.FindFolders(InboxId , new FolderView(10));

    (And this will only work if those credentials have access to the Mailbox in question.


    Friday, August 7, 2015 2:23 AM
  • Hi Glen

    Maybe it's me, but I'm not sure you understand my issue as the answer you've given doesn't seem to answer it. Or at least not in a way that makes sense.

    I've taken the Exchange examples available at the link provided in the question and I've loaded the one called "Exchange 2013 Create weekly recurring appointments on Exchange servers"

    By default this example loads a command prompt, asks for email address and password and uses this to connect to EWS and save a reoccuring calender entry. This works, it saves it to correct calender.

    So, what I've tried to do is change the program so that instead of prompting it looks at the current logged on user to obtain the required credentials and e-mail address.

    So instead of doing this:

    service.Credentials = new NetworkCredential(userData.EmailAddress, userData.Password);

    The code is now doing this:

    Uri uri = new Uri("");
    ICredentials credentials = CredentialCache.DefaultCredentials;
    NetworkCredential credential = credentials.GetCredential(uri, "Basic");
    service.Credentials = credential;

    Now before someone points it out I know I could do this:

    service.UseDefaultCredentials = true;

    Then I also changed the service.AutodiscoverUrl call to use UserPrincipal.Current.EmailAddress instead of userData.EmailAddress.

    Those are the only sections of the code I changed, everything else is the same.

    I used the Folder related code as a quick test to see what inbox folders were being returned and if they were atleast the current users inbox. Maybe putting that into the question was a red herring as it's not the primary issue just a quick test I did on the side.

    Now my understanding of the logic is that this all equates to a change that is simply eliminating the need for my to type in my email and password. Yet for some reason this results in the program saving to the calender of a different user. 

    So, my question is Why? and how can I stop it?


    To give you a feel for what the overall object of this is, I am looking at creating a holiday booking system that works by using pass through authentication on domain pc's. This then connects to EWS and posts holidays that have been approved both to the requesting users calender and to the general shared holiday calender. I want to user pass through authentication for the domain pc's so people don't have to log in when they are in the office but so they can also log in when on Mac's as my understanding is that it just prompts for usernames and passwords if the connection is domain based.

    Friday, August 7, 2015 8:54 AM
  • So what is the context for your application eg are you building Web Service or a client application ? With OnPrem Exchange EWS support Basic or Integrated (NTLM) auth. If you want to use the currently logged on users credentials then you need to be using Integrated Auth. The best way to first test this will all work is try testing it with the EWSEditor . If you trying to get Impersonation to work in Web Service I would suggest looking at the following sample


    Monday, August 10, 2015 11:09 AM
  • Hi Glen,

    Currently I'm just building a console application but with the look to extract the EWS integration and put it into a webservice/website.

    I had a look at the EWSEditor tool and that is behaving in the same way as my code, it is giving me the wrong mail box. When I connect it gives me the mailbox for a different user, even though in the Debug Log Viewer it says it is trying to load the correct one. Then I tried to add a specific folder by selecting the folder and providing an SMTP e-mail address (in fact mine) and this throws an exception.

    Exception details:
    Message: The specified folder could not be found in the store.
    Type: Microsoft.Exchange.WebServices.Data.ServiceResponseException
    Source: Microsoft.Exchange.WebServices
    ErrorCode: ErrorFolderNotFound

    This is the request that is getting sent which results in the error.

    - <Trace Tag="EwsRequest" Tid="1" Time="2015-08-11 13:42:25Z" Version="15.00.0847.030">
      <?xml version="1.0" encoding="utf-8" ?> 
    - <soap:Envelope xmlns:xsi="" xmlns:m="" xmlns:t="" xmlns:soap="">
    - <soap:Header>
      <t:RequestServerVersion Version="Exchange2013_SP1" /> 
    - <soap:Body>
    - <m:GetFolder>
    - <m:FolderShape>
    - <t:AdditionalProperties>
      <t:FieldURI FieldURI="folder:DisplayName" /> 
      <t:FieldURI FieldURI="folder:ChildFolderCount" /> 
    - <m:FolderIds>
    - <t:DistinguishedFolderId Id="calendar">
    - <t:Mailbox>

    So, my next question is, as my Exchange knowledge is limited, what should I be asking my IT department to check because this feels like it could be an issue with exchange itself? Or am I mistaken?

    FYI: Outlook is capable of loading my mailbox and calender without any issues at all. It can also load 2 other exchange accounts all of which seem to function without any problems. One of those accounts is the one that EWS seems to be loading instead of my own.

    Tuesday, August 11, 2015 1:53 PM
  • So from the behaviour your describing I can only surmise that your submitting the credentials of the other user to the Exchange server. You could verify this by looking at the IIS logs on the server for the requests coming from your machine the logs will tell you the authentication context that was used. (You could also use something like fiddler and catch and decode the authentication headers).

    One things to look at is that the EWS Managed API (which the EWSEditor also uses) just uses Httpwebrequest under the covers it could be that your getting the wrong credentials returned from the credentials manager eg see this sounds like it maybe what's happening in your context.

    One suggestion I would have is create a testuser in Exchange/AD and then logon to your PC using that testuser and then try running EWSEditor with that user. Does this work okay ? Most developers have an array different software installed it maybe some other third party proxy or debug software that might be causing this behaviour so I would first try and reproduce it on a Normal non development SOE workstation (eg just do a simple test with EWSEditor).

    I would doubt this is an Exchange/EWS issue (as I've never heard or seen anybody report that) and more likely a specific environmental issue so I would start with the machine your running it on.



    Wednesday, August 12, 2015 5:11 AM