public void RequestAccessTokenAndUserEmail()
        {
            if (string.IsNullOrWhiteSpace(AuthorizationCode))
            {
                throw new Exception("Authorization code is not existed!");
            }

            string requestData  = _provider.GenerateAccessTokenRequestData(AuthorizationCode);
            string responseText = _postString(_provider.TokenUri, requestData);

            OAuthResponseParser parser = new OAuthResponseParser();

            parser.Load(responseText);

            _provider.AccessToken           = parser.AccessToken;
            _provider.RefreshToken          = parser.RefreshToken;
            _provider.UserEmail             = parser.EmailInIdToken;
            _provider.TokenExpiresInSeconds = parser.TokenExpiresInSeconds;

            if (string.IsNullOrWhiteSpace(_provider.AccessToken))
            {
                throw new Exception("Failed to request access token!");
            }

            if (string.IsNullOrWhiteSpace(_provider.UserEmail))
            {
                throw new Exception("Failed to request user email address!");
            }
        }
        public void RefreshAccessToken()
        {
            if (string.IsNullOrWhiteSpace(_provider.RefreshToken))
            {
                throw new Exception("Refresh token is not existed!");
            }

            string requestData  = _provider.GenerateRefreshTokenRequestData();
            string responseText = _postString(_provider.TokenUri, requestData);

            OAuthResponseParser parser = new OAuthResponseParser();

            parser.Load(responseText);

            _provider.AccessToken = parser.AccessToken;
            if (!string.IsNullOrEmpty(parser.RefreshToken))
            {
                _provider.RefreshToken = parser.RefreshToken;
            }

            if (parser.TokenExpiresInSeconds > 0)
            {
                _provider.TokenExpiresInSeconds = parser.TokenExpiresInSeconds;
            }
        }
Esempio n. 3
0
        public void ReceiveMailByOauth()
        {
            try
            {
                string client_id     = "3ac1c2b8-9518-4f7d-9520-f886515364d6"; // "8f54719b-4070-41ae-91ad-f48e3c793c5f";
                string client_secret = "s3y39]smt_rQ5D.-WXbol0RV6=9mFUaz";     // "cbmYyGQjz[d29wL2ArcgoO7HLwJXL/-.";

                // If your application is not created by Office365 administrator,
                // please use Office365 directory tenant id, you should ask Offic365 administrator to send it to you.
                // Office365 administrator can query tenant id in https://portal.azure.com/ - Azure Active Directory.
                string tenant = "b5b8b483-5597-4ae7-8e27-fcc464a3b584";// "79a42c6f-5a9a-439b-a2ca-7aa1b0ed9776";
                string scopes = "https://outlook.office.com/EWS.AccessAsUser.All%20offline_access%20email%20openid";
                //string scopes = "https://outlook.office365.com/EWS.AccessUserEmail";

                string requestData =
                    string.Format("client_id={0}&client_secret={1}&scope={2}",
                                  client_id, client_secret, scopes);


                string tokenUri = string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/token", tenant);
                //string tokenUri = string.Format("https://login.microsoftonline.com/{0}", tenant);
                string responseText = _postString(tokenUri, requestData);

                OAuthResponseParser parser = new OAuthResponseParser();
                parser.Load(responseText);

                // Create a folder named "inbox" under current directory
                // to save the email retrieved.
                string localInbox = string.Format("{0}\\inbox", Directory.GetCurrentDirectory());
                // If the folder is not existed, create it.
                if (!Directory.Exists(localInbox))
                {
                    Directory.CreateDirectory(localInbox);
                }

                string officeUser = "******";
                string token      = parser.AccessToken;
                //token = "eyJ0eXAiOiJKV1QiLCJub25jZSI6Imhhc2VaaEZUQ3gtY1RmVFM4ZGtUN1lydVEzQ3BwUFl5ZE1xUDdjT3d1RFkiLCJhbGciOiJSUzI1NiIsIng1dCI6IllNRUxIVDBndmIwbXhvU0RvWWZvbWpxZmpZVSIsImtpZCI6IllNRUxIVDBndmIwbXhvU0RvWWZvbWpxZmpZVSJ9.eyJhdWQiOiJodHRwczovL291dGxvb2sub2ZmaWNlLmNvbSIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0L2I1YjhiNDgzLTU1OTctNGFlNy04ZTI3LWZjYzQ2NGEzYjU4NC8iLCJpYXQiOjE1ODUxNDA4NzgsIm5iZiI6MTU4NTE0MDg3OCwiZXhwIjoxNTg1MTQ0Nzc4LCJhY2N0IjowLCJhY3IiOiIxIiwiYWlvIjoiQVRRQXkvOE9BQUFBaTFCQTFnQmw0dXpCRkVIOTlVWnEzTjIwRFFUUjkwZjM2eU1DNkoxeWRqNzkxRjZSZzJ5YnlFNUs1RzZ0WWVRUCIsImFtciI6WyJwd2QiXSwiYXBwX2Rpc3BsYXluYW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l2AvbJGiBF1tZSBhrKEjlpPtYNZyE_oqTtiB0G06ZsoDac37ZatPqKq3OhraYh4OO7VYM8WNCvoeebwHIxe3PZZYRuFCZ-Vu2E3HtkiXFduxXQlcsKXFvEI136xQXMPYw1onSGEybMiz5HYsPPTVHx0PW4WEAyb4tiTNtby2vfFxbMFUROkZhwvRzeQgsHXYuafer2ebiWHapPOmkIbrXHnIvyjEJH8vSg4gGRW2HWvDyLxwR5UhZDcn_if0wa16qdDkFz7qzg";
                // use SSL EWS + OAUTH 2.0
                MailServer oServer = new MailServer("outlook.office365.com", officeUser, token, true,
                                                    ServerAuthType.AuthXOAUTH2, ServerProtocol.ExchangeEWS);

                Console.WriteLine("Connecting server ...");

                MailClient oClient = new MailClient("TryIt");
                oClient.Connect(oServer);

                Console.WriteLine("Retreiving email list ...");
                MailInfo[] infos = oClient.GetMailInfos();
                Console.WriteLine("Total {0} email(s)", infos.Length);

                for (int i = 0; i < infos.Length; i++)
                {
                    Console.WriteLine("Checking {0}/{1} ...", i + 1, infos.Length);
                    MailInfo info = infos[i];

                    // Generate an unqiue email file name based on date time.
                    string fileName = _generateFileName(i + 1);
                    string fullPath = string.Format("{0}\\{1}", localInbox, fileName);

                    Console.WriteLine("Downloading {0}/{1} ...", i + 1, infos.Length);
                    Mail oMail = oClient.GetMail(info);

                    // Save mail to local file
                    oMail.SaveAs(fullPath, true);

                    // Mark the email as deleted on server.
                    Console.WriteLine("Deleting ... {0}/{1}", i + 1, infos.Length);
                    oClient.Delete(info);
                }

                Console.WriteLine("Disconnecting ...");

                // Delete method just mark the email as deleted,
                // Quit method expunge the emails from server permanently.
                oClient.Quit();

                Console.WriteLine("Completed!");
            }
            catch (Exception ep)
            {
                Console.WriteLine("Error: {0}", ep.Message);
            }
        }
Esempio n. 4
0
        async void DoOauthAndRetrieveEmail()
        {
            // Creates a redirect URI using an available port on the loopback address.
            string redirectUri = string.Format("http://127.0.0.1:{0}/", GetRandomUnusedPort());

            Console.WriteLine("redirect URI: " + redirectUri);

            // Creates an HttpListener to listen for requests on that redirect URI.
            var http = new HttpListener();

            http.Prefixes.Add(redirectUri);
            Console.WriteLine("Listening ...");
            http.Start();

            // Creates the OAuth 2.0 authorization request.
            string authorizationRequest = string.Format("{0}?response_type=code&scope={1}&redirect_uri={2}&client_id={3}",
                                                        authUri,
                                                        scope,
                                                        Uri.EscapeDataString(redirectUri),//"urn:ietf:wg:oauth:2.0:oob",
                                                        clientID
                                                        );

            // Opens request in the browser.
            System.Diagnostics.Process.Start(authorizationRequest);

            // Waits for the OAuth authorization response.
            var context = await http.GetContextAsync();

            // Brings the Console to Focus.
            BringConsoleToFront();

            // Sends an HTTP response to the browser.
            var    response       = context.Response;
            string responseString = string.Format("<html><head></head><body>Please return to the app and close current window.</body></html>");
            var    buffer         = Encoding.UTF8.GetBytes(responseString);

            response.ContentLength64 = buffer.Length;
            var  responseOutput = response.OutputStream;
            Task responseTask   = responseOutput.WriteAsync(buffer, 0, buffer.Length).ContinueWith((task) =>
            {
                responseOutput.Close();
                http.Stop();
                Console.WriteLine("HTTP server stopped.");
            });

            // Checks for errors.
            if (context.Request.QueryString.Get("error") != null)
            {
                Console.WriteLine(string.Format("OAuth authorization error: {0}.", context.Request.QueryString.Get("error")));
                return;
            }

            if (context.Request.QueryString.Get("code") == null)
            {
                Console.WriteLine("Malformed authorization response. " + context.Request.QueryString);
                return;
            }

            // extracts the code
            var code = context.Request.QueryString.Get("code");

            Console.WriteLine("Authorization code: " + code);

            string responseText = await RequestAccessToken(code, redirectUri);

            Console.WriteLine(responseText);

            OAuthResponseParser parser = new OAuthResponseParser();

            parser.Load(responseText);

            var user        = parser.EmailInIdToken;
            var accessToken = parser.AccessToken;

            Console.WriteLine("User: {0}", user);
            Console.WriteLine("AccessToken: {0}", accessToken);

            RetrieveMailWithXOAUTH2(user, accessToken);
        }