3

I have to establish connectivity with Microsoft Exchange Webservice and I have been given the below details -
Shared mailbox address is say -

"students@student.edu"

Service account is say -

"Student SA"

Password for service account is say -

"Pass1234"

I followed the code sample given in website:

https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications

Below is my code sample using the above details -

static void Main(string[] args)
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
    
    service.Credentials = new WebCredentials("Student SA", "Pass1234");

    service.TraceEnabled = true;
    service.TraceFlags = TraceFlags.All;

    service.AutodiscoverUrl("students@student.edu", RedirectionUrlValidationCallback);

    // service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.PrincipalName, "myADAccount");    
          
}

private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
    // The default for the validation callback is to reject the URL.
    bool result = false;

    Uri 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;
}

When I run this locally, I am getting below error messages

<Trace Tag="AutodiscoverConfiguration" Tid="9" Time="2018-06-04 15:10:07Z">  
     Request error: The remote server returned an error: (401) Unauthorized.  
 </Trace> 

I looked for the same in other threads here How to connect to Exchange?

and also on Code project, but they all tell the same way on how to connect to exchange webservice.

I am not sure as to why I am getting the unauthorized access in Autodiscover configuration and if I am using the correct code to connect to exchange server using the service account information that has been provided.

Ash K
  • 1,802
  • 17
  • 44
Kristy
  • 279
  • 6
  • 18
  • Have you tried [https://testconnectivity.microsoft.com](https://testconnectivity.microsoft.com/#&&B3mVFkrRTjtVd9gvY/ykStceqaBTOMnoH9T/2wjLLXSMX1WX+ainAdR0WcjAPR8PxXIGjVkJ9bhkQAofvsHJM3HCFvAUY53GHd27OBFJ57b25ezc/qN0ifBJeY6XChv4a2b2RQp+9J2uATaPCaX3ibyBtZrFnyNAvLa0atAJUG4fIkWgxkD3jfDpMIVmO/XzjUzu5A==) to verify AutoDiscover works for the account? – tjhazel Jun 04 '18 at 22:51
  • Did you fix this? If yes, can you please share details on how you fixed it? – Ash K Mar 11 '22 at 17:35

2 Answers2

0

Your credential format doesn't look correct you should be either using the downlevel format which would be domain\username or the UPN see https://msdn.microsoft.com/en-us/library/windows/desktop/aa380525(v=vs.85).aspx. I'd suggest you use the UPN as that should always work.

Glen Scales
  • 20,495
  • 1
  • 20
  • 23
0

Try the address instead of the Service Account:

service.Credentials = new WebCredentials("students@student.edu", "Pass1234");

Also this basic authentication is going away. Look into implementing OAuth:

https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth

Ash K
  • 1,802
  • 17
  • 44