Exchange Web Service (EWS) call fails in ASP.NET but not console application

I get an error when trying to connect to Exchange Web Services through ASP.NET.

The following code works if I call it through a console application, but the same code does not execute when executed on an ASP.NET web form page. As a note, I use my own credentials throughout the entire code.

"When creating a request as an account that does not have a mailbox, you must specify the primary SMTP address of the mailbox for any identifiers of the selected folder.

I thought I could fix the problem by specifying an impersonated user.

exchangeservice.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, " email@domain.com "); 

But then I get another error.

"The account does not have permission to impersonate the requested user."

The application pool that the web application runs on is also my own account (just like the console application), so I don’t know what might cause this problem.

I am using the .NET framework 3.5.

Here is the complete code.

 var exchangeservice = new ExchangeService(ExchangeVersion.Exchange2010_SP1) { Timeout = 10000 }; var credentials = new System.Net.NetworkCredential("username", "pass", "domain"); exchangeservice.AutodiscoverUrl(" email@domain.com ") FolderId rootFolderId = new FolderId(WellKnownFolderName.Inbox); var folderView = new FolderView(100) { Traversal = FolderTraversal.Shallow }; FindFoldersResults findFoldersResults = service.FindFolders(rootFolderId, folderView); 
+4
source share
2 answers

Have you tried to directly configure the credentials for the service?

 this.exchangeService.Credentials = new WebCredentials("username", "pass"); this.exchangeService.AutodiscoverUrl("username", this.RedirectionUrlValidationCallback); 

and using this as a validation callback.

  /// <summary> /// Redirections the URL validation callback. /// </summary> /// <param name="redirectionUrl">The redirection URL.</param> /// <returns>true if https</returns> private bool RedirectionUrlValidationCallback(string redirectionUrl) { // The default for the validation callback is to reject the URL. var result = false; var redirectionUri = new Uri(redirectionUrl); // Validate the contents of the redirection URL. In this simple validation // callback, the redirection URL is considered valid if it is using HTTPS // to encrypt the authentication credentials. if (redirectionUri.Scheme == "https") { result = true; } return result; } 
+1
source

Check your credentials and see if the user has sufficient permission to give themselves away. Checking the EWS regulatory policy and providing the correct resolution will solve the problem.

+1
source

All Articles