Search code examples
oauth-2.0azure-active-directoryexchangewebservices

Exchange Webservice using Oauth throws error when subscribing a resource


I am using OAuth2.0 to connect to Exchange webservices. Everything else seems to work ok for me . However when i try to subscribe one of the room resource by using grouping info and providing the anchor mailbox as one of the primary mail box it throws an error.

"Request failed because EWS could not contact the appropriate CAS server for this request."

So for example i am trying to subscribe nitroom1 and one the primary mailbox associated with the group is nitroom2 which i am using as X-AnchorMailbox then i got the above error.

public static ExchangeService GetExchangeService(string exchangeURL, string userName, string password, string resourceEmail, string primaryMailbox, string clientID, string tenantID, string clientSecret, string certName)
    {
        ExchangeService service;
        service = new ExchangeService(setTZtoUTC);
        service.Url = new Uri(exchangeURL);
        if (!string.IsNullOrWhiteSpace(clientID) && !string.IsNullOrWhiteSpace(tenantID))
        {
            string oAuthToken = multiExchangeManager.getOAuthTokenFromCache(clientID, tenantID, clientSecret, certName);
            service.Credentials = new OAuthCredentials(oAuthToken);
        }
        else
        {
            service.Credentials = new WebCredentials(userName, password);
        }
        service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, resourceEmail);
        service.HttpHeaders.Add("X-AnchorMailbox", primaryMailbox);
        service.HttpHeaders.Add("X-PreferServerAffinity", "true");
        return service;
    }

However if i connect ews using impersonate account then do same thing it works fine.

Also, if i use resourceMailbox same as primary mailbox then it works ok as well.so in my example it will look like this.

 service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "nitroom1");
        service.HttpHeaders.Add("X-AnchorMailbox", "nitroom1");

This is how i am trying to use subscription.

exchangeService.SubscribeToStreamingNotifications(
                new FolderId[] { WellKnownFolderName.Calendar, WellKnownFolderName.DeletedItems },
                EventType.Created, EventType.Deleted, EventType.Modified, EventType.Moved, EventType.Copied);

Does anyone have any idea why its happening or what i am doing wrong here?

one more thing to add, i tried EWSEditor tool which provides subscription info and both above mentioned resources sharing same grouping info.


Solution

  • I think i found a solution for this issue, i just need to set X-BackEndOverRideCookie with any service used for subscribing child mailbox.

    For more info read this article https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-maintain-affinity-between-group-of-subscriptions-and-mailbox-server