5

I have attempted this with not much success. Basically I need to login to Exchange using EWS remotely.

The issue is I don't know if the user has logged in OK or if the credentials are wrong as I get nothing back! If I provide wrong credentials the software just carries on!

Is there something I'm missing, I've checked the MSDN stuff about EWS which shows you how to connect to exchange but nothing about validating credentials!

Below is the code I currently have to connect.

public void connect(string Email, string Password)
    {
        try
        {            
            _useremail = Email;
            _userpass = Password;

            // Define the credentials to use.
            var credentials = new WebCredentials(_useremail, _userpass);

            _ExchangeServer = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
            _ExchangeServer.Credentials = credentials;
            _ExchangeServer.Url = new Uri(_ExchangeEWSURL);
            _ExchangeServer.Timeout = 60;
            _ExchangeServer.KeepAlive = true;
            _ExchangeConnected = true;
        }
        catch (Exception ex)
        {
            _ExchangeConnected = false;
            throw ex;
        }
    }

as you can see at present I just set a bool value to true in the class. Any ideas?

Neo
  • 2,305
  • 4
  • 36
  • 70

1 Answers1

11

In order to check whether the given credentials are valid, you must query resources you expect the user to have access to (calendar, inbox, contacts, etc.). There is no explicit login method - the authentication occurs implicitly when you request user resources (via FindItems, FindFolders, FindAppointments, etc.).

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • 1
    Thanks SliverNinja (great name btw) from what I have read could I do a PrincipalContext? Sadly the server is giving me 401s at the moment so I can't test :( – Neo Apr 30 '12 at 14:46
  • 3
    If you just want to ensure that the credentials are valid and not test for access to specific resources, you can make a call like this `exchService.ConvertIds(new AlternateId[] { new AlternateId(IdFormat.HexEntryId, "00", email) }, IdFormat.HexEntryId);` It'll thorw an exception if the credentilas are wrong – Rune Andersen Aug 20 '14 at 13:21
  • There's a bit of sample code for using FindFolders() here: https://stackoverflow.com/questions/36587942/validate-login-credentials-using-ews-managed-api – RenniePet Sep 23 '17 at 00:27