예제 #1
0
        public async Task <ExchangeAuthorizationResult> LoginAsync(ExchangeConnectionInfo connectionInfo)
        {
            GetFolderIdentifiersResult result;

            var autoDiscoverResult = await this.EnsureAutoDiscover(connectionInfo);

            if (!IsExchangeSyncResultValid(autoDiscoverResult))
            {
                return(autoDiscoverResult.AuthorizationResult);
            }

            try
            {
                var server = new EwsSyncServer(connectionInfo.CreateEwsSettings());
                result = await server.GetRootFolderIdentifiersAsync(useCache : false);
            }
            catch (Exception e)
            {
                return(HandleLoginException(e));
            }

            if (result != null && result.TaskFolderIdentifier != null)
            {
                return(new ExchangeAuthorizationResult
                {
                    AuthorizationStatus = ExchangeAuthorizationStatus.OK,
                    IsOperationSuccess = true,
                    ServerUri = connectionInfo.ServerUri
                });
            }
            else
            {
                return(new ExchangeAuthorizationResult
                {
                    AuthorizationStatus = ExchangeAuthorizationStatus.Unkown,
                    ErrorMessage = "Unable to find mailbox identifiers",
                    IsOperationSuccess = false
                });
            }
        }
예제 #2
0
        private async Task <ExchangeSyncResult> EnsureAutoDiscover(ExchangeConnectionInfo connectionInfo)
        {
            var success = new ExchangeSyncResult
            {
                AuthorizationResult = new ExchangeAuthorizationResult
                {
                    AuthorizationStatus = ExchangeAuthorizationStatus.OK,
                    IsOperationSuccess  = true
                },
                OperationResult = new ServiceOperationResult
                {
                    IsOperationSuccess = true
                },
            };

            // check Office 365 server uri is correct (ie. https://outlook.office365.com/EWS/Exchange.asmx)
            if (connectionInfo.ServerUri != null)
            {
                string uri = connectionInfo.ServerUri.ToString().ToLowerInvariant();
                if (uri.Contains("outlook.office365.com") && !uri.EndsWith("/ews/exchange.asmx"))
                {
                    connectionInfo.ServerUri = SafeUri.Get("https://outlook.office365.com/EWS/Exchange.asmx");
                }

                if (uri.Contains("/ews/exchange.asmx/ews/exchange.asmx"))
                {
                    connectionInfo.ServerUri = SafeUri.Get(uri.Replace("/ews/exchange.asmx/ews/exchange.asmx", "/EWS/Exchange.asmx"));
                }

                // check if the server uri looks correct, if it doesn't, run autodiscover
                var server = new EwsSyncServer(connectionInfo.CreateEwsSettings());
                try
                {
                    var identifiers = await server.GetRootFolderIdentifiersAsync(useCache : false);

                    if (identifiers != null)
                    {
                        return(success);
                    }
                }
                catch (Exception ex)
                {
                    if (connectionInfo.ServerUri != null && !connectionInfo.ServerUri.ToString().EndsWith("/ews/exchange.asmx", StringComparison.OrdinalIgnoreCase))
                    {
                        // one more try with the /ews/exchange.asmx path
                        connectionInfo.ServerUri = SafeUri.Get(uri.TrimEnd('/') + "/ews/exchange.asmx");
                        var secondResult = await this.EnsureAutoDiscover(connectionInfo);

                        if (IsExchangeSyncResultValid(secondResult))
                        {
                            return(success);
                        }
                    }
                    LogService.Log("EwsSyncService", $"Server uri is {connectionInfo.ServerUri} but GetRootFolderIds failed ({ex.Message}), fallbacking to classic autodiscover");
                }
            }

            LogService.Log("EwsSyncService", "Server uri is empty, performing auto discover");

            var engine             = new AutoDiscoverEngine();
            var autoDiscoverResult = await engine.AutoDiscoverAsync(connectionInfo.Email, connectionInfo.Username, connectionInfo.Password, connectionInfo.Version);

            if (autoDiscoverResult != null)
            {
                if (autoDiscoverResult.ServerUri != null)
                {
                    LogService.Log("EwsSyncService", "Now using server uri: " + autoDiscoverResult.ServerUri);
                    connectionInfo.ServerUri = autoDiscoverResult.ServerUri;
                }

                if (!string.IsNullOrWhiteSpace(autoDiscoverResult.RedirectEmail))
                {
                    LogService.Log("EwsSyncService", "Now using email redirect: " + autoDiscoverResult.RedirectEmail);
                    connectionInfo.Email = autoDiscoverResult.RedirectEmail;
                }
                return(success);
            }

            LogService.Log("EwsSyncService", "Auto discovery failed");

            return(new ExchangeSyncResult
            {
                AuthorizationResult = new ExchangeAuthorizationResult
                {
                    AuthorizationStatus = ExchangeAuthorizationStatus.AutodiscoveryServiceNotFound,
                    ErrorMessage = "Check credentials are valid and/or try to specify manually a valid server address"
                }
            });
        }
예제 #3
0
        public async Task <ExchangeSyncResult> ExecuteSyncAsync(ExchangeConnectionInfo connectionInfo, ExchangeChangeSet changeSet, string syncState, string folderId)
        {
            if (connectionInfo == null)
            {
                throw new ArgumentNullException(nameof(connectionInfo));
            }
            if (changeSet == null)
            {
                throw new ArgumentNullException(nameof(changeSet));
            }

            var outChangeSet = new ExchangeChangeSet();

            var autoDiscoverResult = await this.EnsureAutoDiscover(connectionInfo);

            if (!IsExchangeSyncResultValid(autoDiscoverResult))
            {
                return(autoDiscoverResult);
            }

            var server = new EwsSyncServer(connectionInfo.CreateEwsSettings());

            var identifiers = await server.GetRootFolderIdentifiersAsync();

            //var subFolders = await server.GetSubFoldersAsync(identifiers.TaskFolderIdentifier);

            //var flaggedItemFolders = await this.EnsureSearchFolder(server);

            /*
             * var a = await server.EnumerateFolderContentAsync(flaggedItemFolders);
             * var d = await server.DownloadFolderContentAsync(a);
             */

            var mapId = new List <ExchangeMapId>();

            await this.PushLocalChanges(changeSet, server, mapId, identifiers.TaskFolderIdentifier);

            var remoteIdentifiers = await this.ApplyRemoteChanges(outChangeSet, server, mapId, identifiers.TaskFolderIdentifier, EwsItemType.Task);

            /*foreach (var subFolder in subFolders.Folders)
             * {
             *  await this.ApplyRemoteChanges(outChangeSet, server, mapId, subFolder);
             * }*/

            if (connectionInfo.SyncFlaggedItems)
            {
                try
                {
                    var identifier = await this.EnsureSearchFolderAsync(server);

                    var otherRemoteIdentifiers = await this.ApplyRemoteChanges(outChangeSet, server, mapId, identifier, EwsItemType.Item);

                    remoteIdentifiers.AddRange(otherRemoteIdentifiers);
                }
                catch (Exception ex)
                {
                    LogService.Log("EwsSyncService", $"Exception while syncing flagged items: {ex}");
                    TrackingManagerHelper.Exception(ex, "Exception while syncing flagged items");
                }
            }

            // check for deleted items
            foreach (var task in this.workbook.Tasks.Where(t => t.SyncId != null))
            {
                var remoteId = remoteIdentifiers.FirstOrDefault(i => i.Id == task.SyncId.GetId());
                if (remoteId == null)
                {
                    // local item not found on server because it was deleted => delete
                    outChangeSet.DeletedTasks.Add(new ServerDeletedAsset(task.SyncId));
                }
            }

            var result = new ExchangeSyncResult
            {
                AuthorizationResult = new ExchangeAuthorizationResult
                {
                    Status = "OK",
                    AuthorizationStatus = ExchangeAuthorizationStatus.OK,
                    IsOperationSuccess  = true,
                    ServerUri           = connectionInfo.ServerUri
                },
                ChangeSet       = outChangeSet,
                MapId           = mapId,
                SyncState       = identifiers.TaskFolderIdentifier.ChangeKey,
                OperationResult = new ServiceOperationResult
                {
                    IsOperationSuccess = true
                }
            };

            return(result);
        }