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 }); }
public static ExchangeMailboxAuditGroupRecord CreateMailboxGroupRecord(MailboxSession mailboxSession, MailboxAuditOperations operation, COWSettings settings, LogonType effectiveLogonType, bool externalAccess, StoreSession destinationSession, StoreObjectId destinationFolderId, StoreObjectId[] itemIds, GroupOperationResult result, IDictionary <StoreObjectId, FolderAuditInfo> folders, IDictionary <StoreObjectId, ItemAuditInfo> items, IDictionary <StoreObjectId, FolderAuditInfo> parentFolders) { Util.ThrowOnNullArgument(settings, "settings"); ExchangeMailboxAuditGroupRecord exchangeMailboxAuditGroupRecord = new ExchangeMailboxAuditGroupRecord(); AuditRecordFactory.Fill(exchangeMailboxAuditGroupRecord, mailboxSession, operation, (result == null) ? OperationResult.Failed : result.OperationResult, effectiveLogonType, externalAccess); if (settings.CurrentFolderId != null) { exchangeMailboxAuditGroupRecord.Folder = (exchangeMailboxAuditGroupRecord.Folder ?? new ExchangeFolder()); exchangeMailboxAuditGroupRecord.Folder.Id = string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { settings.CurrentFolderId }); FolderAuditInfo folderAuditInfo; parentFolders.TryGetValue(settings.CurrentFolderId, out folderAuditInfo); exchangeMailboxAuditGroupRecord.Folder.PathName = ((folderAuditInfo != null) ? folderAuditInfo.PathName : AuditRecordFactory.GetCurrentFolderPathName(mailboxSession, settings)); } bool flag = destinationSession != null && mailboxSession != destinationSession; exchangeMailboxAuditGroupRecord.CrossMailboxOperation = new bool?(flag); MailboxSession mailboxSession2 = mailboxSession; if (flag && destinationSession is MailboxSession) { mailboxSession2 = (destinationSession as MailboxSession); exchangeMailboxAuditGroupRecord.DestMailboxGuid = new Guid?(mailboxSession2.MailboxOwner.MailboxInfo.MailboxGuid); exchangeMailboxAuditGroupRecord.DestMailboxOwnerUPN = string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { mailboxSession2.MailboxOwner.MailboxInfo.PrimarySmtpAddress }); if (mailboxSession2.MailboxOwner.Sid != null) { exchangeMailboxAuditGroupRecord.DestMailboxOwnerSid = string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { mailboxSession2.MailboxOwner.Sid }); if (mailboxSession2.MailboxOwner.MasterAccountSid != null) { exchangeMailboxAuditGroupRecord.DestMailboxOwnerMasterAccountSid = string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { mailboxSession2.MailboxOwner.MasterAccountSid }); } } } if (destinationFolderId != null) { exchangeMailboxAuditGroupRecord.DestFolder = (exchangeMailboxAuditGroupRecord.DestFolder ?? new ExchangeFolder()); exchangeMailboxAuditGroupRecord.DestFolder.Id = string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { destinationFolderId }); string text = null; Exception ex = null; try { using (Folder folder = Folder.Bind(mailboxSession2, destinationFolderId, new PropertyDefinition[] { FolderSchema.FolderPathName })) { if (folder != null) { text = (folder.TryGetProperty(FolderSchema.FolderPathName) as string); if (text != null) { text = text.Replace(COWSettings.StoreIdSeparator, '\\'); } } } } catch (StorageTransientException ex2) { ex = ex2; } catch (StoragePermanentException ex3) { ex = ex3; } if (ex != null) { ExTraceGlobals.SessionTracer.TraceError <StoreObjectId, Exception>((long)mailboxSession.GetHashCode(), "[GroupOperationAuditEventRecordAdapter::ToString] failed to get FolderPathName property of destination folder {0}. Exception: {1}", destinationFolderId, ex); } if (text != null) { exchangeMailboxAuditGroupRecord.DestFolder.PathName = text; } } foreach (KeyValuePair <StoreObjectId, FolderAuditInfo> keyValuePair in folders) { StoreObjectId key = keyValuePair.Key; FolderAuditInfo value = keyValuePair.Value; if (settings.CurrentFolderId == null || !key.Equals(settings.CurrentFolderId)) { exchangeMailboxAuditGroupRecord.Folders = (exchangeMailboxAuditGroupRecord.Folders ?? new List <ExchangeFolder>(folders.Count)); ExchangeFolder exchangeFolder = new ExchangeFolder(); exchangeMailboxAuditGroupRecord.Folders.Add(exchangeFolder); exchangeFolder.Id = string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { key }); if (value.PathName != null) { exchangeFolder.PathName = value.PathName; } } } foreach (KeyValuePair <StoreObjectId, ItemAuditInfo> keyValuePair2 in items) { StoreObjectId key2 = keyValuePair2.Key; ItemAuditInfo value2 = keyValuePair2.Value; exchangeMailboxAuditGroupRecord.SourceItems = (exchangeMailboxAuditGroupRecord.SourceItems ?? new List <ExchangeItem>(items.Count)); ExchangeItem exchangeItem = new ExchangeItem(); exchangeMailboxAuditGroupRecord.SourceItems.Add(exchangeItem); exchangeItem.Id = string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { key2 }); if (value2.Subject != null) { exchangeItem.Subject = value2.Subject; } FolderAuditInfo folderAuditInfo2; if (value2.ParentFolderId != null && parentFolders.TryGetValue(value2.ParentFolderId, out folderAuditInfo2)) { exchangeItem.ParentFolder = (exchangeItem.ParentFolder ?? new ExchangeFolder()); exchangeItem.ParentFolder.PathName = folderAuditInfo2.PathName; } } return(exchangeMailboxAuditGroupRecord); }