401 Unauthorized while ExchangeService.SubscribeToPushNotifications RRS feed

  • Question

  • Hi I have written an standalone Windows Forms application which uses EWS Managed API to listen to the new mail event on a particular email id. I am using impersonation. This is a part of my code: 

    service = new ExchangeService(SyncSettings.ExchangeServerVersion);
    service.Credentials = new WebCredentials(userid, password);
    service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, impersonatedUserId);            
    service.Url = new Uri(exchnguri);
    FolderId[] folders=new FolderId[1];
    folders[0] = new FolderId(WellKnownFolderName.Inbox);
    EventType[] events = new EventType[1];
    events[0] = EventType.NewMail; 
    subscription = service.SubscribeToPushNotifications(folders, new Uri(listenUri), notificationInterval, "", events);

    On Exchange Server side, I have provided one user account privileges to impersonate another user account.

    When I run app in one environment it works fine. But when I run it in another it gives following error:

                          Immediate Stack Trace 
    Microsoft.Exchange.WebServices.Data.ServiceRequestException : The request failed. The remote server returned an error: (401) Unauthorized.
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request)
       at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
       at Microsoft.Exchange.WebServices.Data.ExchangeService.SubscribeToPushNotifications(IEnumerable`1 folderIds, Uri url, Int32 frequency, String watermark, EventType[] eventTypes)
       at com.org.app.Class.startListeningPushNotifications(String listenUri, Int32 notificationInterval) in c:\SW\app\Class.cs:line 156
         Inner Exception 1 : Stack Trace
    System.Net.WebException . The remote server returned an error: (401) Unauthorized.
       at System.Net.HttpWebRequest.GetResponse()
       at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse()
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)

    The only difference I observe in two environments is:

    In first environment I login owa (Outlook Web Access) using domainx\userx, and to send mail to self account (that is, to the account with which I have logged in) I put userx@domainx.com in to field

    However in second environment I login owa using domainxdc\userx (notice 'dc' in domainxdc), and send self mail with userx@domainx.com in to field. (notice no 'dc' in domainx.com)

    I dont think that is giving me 401 Unauthorised, since I can login to owa using both impersonating and impersonated account. And I have given impersonating privileges in 2nd environment in exactly the same way as in 1st environment. 

    So why I am getting 401 Unauthorised ?

    • Edited by Mahesha999 Thursday, June 12, 2014 12:04 PM
    Thursday, June 12, 2014 11:59 AM