Example #1
0
        public async Task <bool> ConnectExchange(string authToken)
        {
            bool success = true;

            try
            {
                ThisExchangeService = new ExchangeService
                {
                    Url         = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"),
                    Credentials = new OAuthCredentials(authToken)
                };

                Folder          Inbox         = Folder.Bind(ThisExchangeService, WellKnownFolderName.Inbox);
                AlternateId     aiAlternateid = new AlternateId(IdFormat.EwsId, Inbox.Id.UniqueId, "*****@*****.**");
                AlternateIdBase aiResponse    = ThisExchangeService.ConvertId(aiAlternateid, IdFormat.EwsId);
                ThisMailbox = ((AlternateId)aiResponse).Mailbox;
            }
            catch (Exception ex)
            {
                success = false;
                Logger.FireWindowsLog(WinLogger.ApplicationEventType.Error, "Exchange Operation Failed - " + ex);
            }

            //success = await EnumerateFoldersAsync();
            return(success);
        }
        /// <summary>
        /// Authenticates using the credentials provided to the credential manager. It also
        /// populates the ExchangeService object used by the application.
        /// </summary>
        /// <param name="userprincipalname">The user's name for accessing Exchange</param>
        /// <param name="password">The user's password.</param>
        /// <param name="service">The ExchangeService object for this application.</param>
        /// <returns>A value of true indicates that the client successfully authenticated with EWS.</returns>
        private static bool Authenticate(string userprincipalname, SecureString password, ref ExchangeService service)
        {
            bool authenticated = false;

            try
            {
                service.Credentials = new NetworkCredential(userprincipalname, password);

                // Check if we have the service URL.
                if (service.Url == null)
                {
                    Console.WriteLine("Using Autodiscover to find EWS URL for {0}. Please wait... ", userprincipalname);
                    service.AutodiscoverUrl(userprincipalname, Service.RedirectionUrlValidationCallback);
                    Console.WriteLine("Autodiscover complete.");
                }

                // Once we have the URL, try a ConvertId operation to check if we can access the service. We expect that
                // the user will be authenticated and that we will get an error code due to the invalid format. Expect a
                // ServiceResponseException.
                Console.WriteLine("Attempting to connect to EWS...");
                AlternateIdBase response = service.ConvertId(new AlternateId(IdFormat.EwsId, "Placeholder", userprincipalname), IdFormat.EwsId);
            }

            // The user principal name is in a bad format.
            catch (FormatException fe)
            {
                Console.WriteLine("Error: {0} Please enter your credentials in UPN format.", fe.Message);
                //service = null;
            }

            catch (AutodiscoverLocalException ale)
            {
                Console.WriteLine("Error: {0}", ale.Message);
                //service = null;
            }

            // The credentials were authenticated. We expect this exception since we are providing intentional bad data for ConvertId
            catch (ServiceResponseException)
            {
                Console.WriteLine("Successfully connected to EWS.");
                authenticated = true;
            }

            // The credentials were not authenticated.
            catch (ServiceRequestException)
            {
                throw new ApplicationException("The credentials were not authenticated.");
                //service = null;
            }

            return(authenticated);
        }
Example #3
0
        /// <summary>
        /// Authenticates using the credentials provided to the credential manager. It also
        /// populates the ExchangeService object used by the application.
        /// </summary>
        /// <param name="userprincipalname">The user's name for accessing Exchange</param>
        /// <param name="password">The user's password.</param>
        /// <param name="service">The ExchangeService object for this application.</param>
        /// <returns>A value of true indicates that the client successfully authenticated with EWS.</returns>
        private static bool Authenticate(string userprincipalname, SecureString password, ref ExchangeService service)
        {
            bool authenticated = false;

            try
            {
                service.Credentials = new NetworkCredential(userprincipalname, password);

                // Check if we have the service URL.
                if (service.Url == null)
                {
                    Console.WriteLine("Wyszukiwanie EWS URL dla konta {0}. Czekaj... ", userprincipalname);
                    service.AutodiscoverUrl(userprincipalname, Service.RedirectionUrlValidationCallback);
                    Console.WriteLine("Wyszukiwanie zakończone.");
                }

                // Once we have the URL, try a ConvertId operation to check if we can access the service. We expect that
                // the user will be authenticated and that we will get an error code due to the invalid format. Expect a
                // ServiceResponseException.
                Console.WriteLine("Łączenie z EWS...");
                AlternateIdBase response = service.ConvertId(new AlternateId(IdFormat.EwsId, "Placeholder", userprincipalname), IdFormat.EwsId);
            }

            // The user principal name is in a bad format.
            catch (FormatException fe)
            {
                Console.WriteLine("Błąd: {0} Wprowadź dane dostępowe w formacie UPN.", fe.Message);
                //service = null;
            }

            catch (AutodiscoverLocalException ale)
            {
                Console.WriteLine("Błąd: {0}", ale.Message);
                //service = null;
            }

            // The credentials were authenticated. We expect this exception since we are providing intentional bad data for ConvertId
            catch (ServiceResponseException)
            {
                Console.WriteLine("Połączono z EWS.");
                authenticated = true;
            }

            // The credentials were not authenticated.
            catch (ServiceRequestException)
            {
                throw new ApplicationException("Dane dostępowe są nieważne.");
                //service = null;
            }

            return(authenticated);
        }
        public String Get_EwsIdFromEntryID(ExchangeService oExchangeService, String sSmtp, String sEntryID)
        {
            // https://blogs.msdn.microsoft.com/brijs/2010/09/09/how-to-convert-exchange-items-entryid-to-ews-unique-itemid-via-ews-managed-api-convertid-call/

            // Create a request to convert identifiers.
            AlternateId objAltID = new AlternateId();

            objAltID.Format   = IdFormat.EntryId;
            objAltID.Mailbox  = sSmtp;
            objAltID.UniqueId = sEntryID;

            //Convert  PR_ENTRYID identifier format to an EWS identifier.
            AlternateIdBase objAltIDBase = oExchangeService.ConvertId(objAltID, IdFormat.EwsId);
            AlternateId     objAltIDResp = (AlternateId)objAltIDBase;

            return(objAltIDResp.UniqueId);
        }
    void ReadElementsFromXml(EwsServiceXmlReader reader)
    {
        base.ReadElementsFromXml(reader);

        reader.ReadStartElement(XmlNamespace.Messages, XmlElementNames.AlternateId);

        String alternateIdClass = reader.ReadAttributeValue(XmlNamespace.XmlSchemaInstance, XmlAttributeNames.Type);

        int aliasSeparatorIndex = alternateIdClass.IndexOf(':');

        if (aliasSeparatorIndex > -1)
        {
            alternateIdClass = alternateIdClass.Substring(aliasSeparatorIndex + 1);
        }

        // Alternate Id classes are responsible fro reading the AlternateId end element when necessary
        switch (alternateIdClass)
        {
        case AlternateId.SchemaTypeName:
            this.convertedId = new AlternateId();
            break;

        case AlternatePublicFolderId.SchemaTypeName:
            this.convertedId = new AlternatePublicFolderId();
            break;

        case AlternatePublicFolderItemId.SchemaTypeName:
            this.convertedId = new AlternatePublicFolderItemId();
            break;

        default:
            EwsUtilities.Assert(
                false,
                "ConvertIdResponse.ReadElementsFromXml",
                string.Format("Unknown alternate Id class: {0}", alternateIdClass));
            break;
        }

        this.convertedId.LoadAttributesFromXml(reader);

        reader.ReadEndElementIfNecessary(XmlNamespace.Messages, XmlElementNames.AlternateId);
    }