I have Windows Service which listen On-Premise Exchange Mail boxes using EWS with Impersonation Access.
I have one Admin User (Service Account) which has Impersonation Access and I have configured the same user for my Windows Service Logon.
I am using NTLM Authentication in C#.Net to login and Impersonate the mail box. When I start my windows service and try to impersonate the mail box I am getting following error even though I have Impersonation Access to my service account.
“Error While initial sync for mailbox [email protected]. Exception: Microsoft.Exchange.WebServices.Data.ServiceResponseException: The account does not have permission to impersonate the requested user.
at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ProcessWebException(WebException webException)
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 SXA.ES.EWSNotificationListenerService.NotificationListener.NotificationSynchronizerBase.GetCurrentSyncState(String smtpAddress, String autodiscoverUrl)”
Surprise part is, this issue is happening with specific Virtual Machines, where my Windows Service is hosted.
I have total 4 Virtual Machines and out of 4 VMs, Windows Service is working fine and able to Impersonate the mail box on 2 VMs, but with same configuration and same setup other 2 are having above mentioned error.
Here is my code which create OnPremise Exchange Service:
public ExchangeService CreateOnPremiseExchangeService(ExchangeServiceProperties properties)
{
var exchangeService = CreateExchangeService(properties);
exchangeService.UseDefaultCredentials = true;
return exchangeService;
}
private static ExchangeService CreateExchangeService(ExchangeServiceProperties properties)
{
var exchangeService = new ExchangeService(properties.ExchangeVersion)
{
Url = properties.ExchangeUri,
ReturnClientRequestId = true,
SendClientLatencies = true,
UserAgent = properties.UserAgent
};
if (properties.TraceListener != null)
{
exchangeService.TraceListener = properties.TraceListener;
exchangeService.TraceFlags = TraceFlags.All;
exchangeService.TraceEnabled = true;
}
if (!string.IsNullOrWhiteSpace(properties.TargetMailbox))
{
exchangeService.ImpersonatedUserId = new ImpersonatedUserId(
ConnectingIdType.SmtpAddress,
properties.TargetMailbox);
}
return exchangeService;
}
I tried searching over the google a lot for this issue and could not find any post.
I am seeking help here to address this issue. Please let me know if anyone come across the same issue while working with EWS Service with C#.Net, and have solution for this.
Note: If I use Basic Authentication here then it is working fine on these 2 VMs as well, this issue is happening with NTLM Windows Authentication only.
I have further investigated and found the root cause of this issue, see the details below and how to fix the same.
My suggestion is, avoid configuring Outlook on VM. How this will help.