Search code examples
c#exchangewebservicesnetworkcredentials

How can i validate credentials passed to the NetworkCredential constructor for use with EWS?


Is there a way that you can validate credentials passed to System.Net.NetworkCredential class? I want to ensure that calls that are made to Exchange Web Services are valid so that the calls can work.

Here is the example code:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
 service.Credentials = new NetworkCredential(user_id, password);

//how to validate credentials here? 

service.Url = new System.Uri("some_url");
Microsoft.Exchange.WebServices.Data.Appointment meeting1 = new Microsoft.Exchange.WebServices.Data.Appointment(service);

 //add some extra stuff to the meeting object

  ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

 Microsoft.Exchange.WebServices.Data.Mailbox test = new Microsoft.Exchange.WebServices.Data.Mailbox(email);

 Microsoft.Exchange.WebServices.Data.FolderId folderid = new Microsoft.Exchange.WebServices.Data.FolderId(WellKnownFolderName.Calendar, test);

 meeting1.Save(folderid,SendInvitationsMode.SendToAllAndSaveCopy);

Also, is there a way to check if the .Save() actually worked? the function has no return value.


Solution

  • Is there a way that you can validate credentials passed to System.Net.NetworkCredential class?

    These are Active Directory credentials so if you want to validate them you look at something like Validate a username and password against Active Directory? .EWS doesn't provide any methods for credential validateion outside of just returning a 401 if they aren't correct.

    Also, is there a way to check if the .Save() actually worked? the function has no return value

    If an invalid response is received from the server then an exception will be raised so you should have a try/catch at least around the save in this case (exception can be request or response based). With Meetings if the create is successfully in your example the meeting1 object will now have an underlying Id in its Id property.