private ExchangeAuthorizationResult GetFailedAuthResult(string command, SyncCommandResult syncResult) { return(new ExchangeAuthorizationResult { AuthorizationStatus = ExchangeAuthorizationStatus.OK, IsOperationSuccess = false, Status = syncResult.Status, ErrorMessage = $"command: {command} status: {ActiveSyncErrorHelper.GetSyncErrorMessage(syncResult.Status)}" }); }
public async Task <ExchangeAuthorizationResult> LoginAsync(ExchangeConnectionInfo connectionInfo) { if (connectionInfo == null) { throw new ArgumentNullException("connectionInfo"); } ActiveSyncServer server; // reset cached task folder id this.taskFolderId = null; if (connectionInfo.ServerUri == null) { LogService.Log("ActiveSyncService", "Server is empty, trying to auto discover..."); server = CreateExchangeServer(connectionInfo); string discoveredUri = await server.GetAutoDiscoveredServer(); if (string.IsNullOrEmpty(discoveredUri)) { LogService.Log("ActiveSyncService", "Didn't found a server for the email address"); // Wrong credentials passed to the server return(new ExchangeAuthorizationResult { AuthorizationStatus = ExchangeAuthorizationStatus.UserCredentialsInvalid, IsOperationSuccess = false, ErrorMessage = ExchangeResources.ExchangeActiveSync_InvalidCredentialsMessage }); } connectionInfo.ServerUri = SafeUri.Get(discoveredUri); LogService.LogFormat("ActiveSyncService", "Server autodiscovered at '{0}'", discoveredUri); } // Try to get folders information to know if user is correct server = CreateExchangeServer(connectionInfo); FolderSyncCommandResult result; try { result = await server.FolderSync("0"); connectionInfo.PolicyKey = server.PolicyKey; } catch (Exception ex) { string message = string.Empty + ex.Message; if (ex.InnerException != null && ex.Message != null) { message += ", " + ex.InnerException.Message; } return(new ExchangeAuthorizationResult { AuthorizationStatus = ExchangeAuthorizationStatus.UserCredentialsInvalid, IsOperationSuccess = false, ErrorMessage = message }); } if (result.Status != StatusOk) { return(new ExchangeAuthorizationResult { AuthorizationStatus = ExchangeAuthorizationStatus.OK, IsOperationSuccess = false, ErrorMessage = ActiveSyncErrorHelper.GetFolderSyncErrorMessage(result.Status) }); } ExchangeFolder taskFolder = null; if (string.IsNullOrWhiteSpace(this.taskFolderId)) { taskFolder = result.AddedFolders.FirstOrDefault(f => f.FolderType == DefaultTaskFolderType); if (taskFolder == null) { LogService.Log("ActiveSyncService", "No default task folder found"); return(new ExchangeAuthorizationResult { AuthorizationStatus = ExchangeAuthorizationStatus.OK, IsOperationSuccess = false, ErrorMessage = ExchangeResources.ExchangeActiveSync_NoTasksFolder }); } } else { taskFolder = result.AddedFolders.FirstOrDefault(f => f.ServerId == this.taskFolderId); if (taskFolder == null) { taskFolder = result.AddedFolders.FirstOrDefault(f => f.FolderType == DefaultTaskFolderType); string taskFolderName = taskFolder != null ? taskFolder.DisplayName : "unkown"; string message = $"Could not retrieve task folder with id {this.taskFolderId} (default: {taskFolderName}, all: {result.AddedFolders.Select(f => f.DisplayName).AggregateString()})"; LogService.LogFormat("ActiveSyncService", message); return(new ExchangeAuthorizationResult { AuthorizationStatus = ExchangeAuthorizationStatus.OK, IsOperationSuccess = false, ErrorMessage = message }); } else { LogService.LogFormat("ActiveSyncService", "Using task folder: {0}", taskFolder.ServerId); } } this.taskFolderId = taskFolder.ServerId; this.taskFolderName = taskFolder.DisplayName; // all is Ok, update server Uri with the found Uri return(new ExchangeAuthorizationResult { AuthorizationStatus = ExchangeAuthorizationStatus.OK, IsOperationSuccess = true, ServerUri = connectionInfo.ServerUri }); }