How to authenticate with OAuth to access the EWS API

Currently, my web service is performing basic authentication of the username and password to subscribe to the exchange user to receive events (for example, a new mail event, etc.), as shown below:

var service = new ExchangeService(exchangeVersion)
                                  {
                                      KeepAlive = true,
                                      Url = new Uri("some autodiscovery url"),
                                      Credentials = new NetworkCredential(username, password)
                                  };

var subscription = service.SubscribeToPushNotifications(
                                    new[] { inboxFolderFoldeID },
                                    new Uri("some post back url"),
                                    15,
                                    null,
                                    EventType.NewMail,
                                    EventType.Created,
                                    EventType.Deleted,
                                    EventType.Modified,
                                    EventType.Moved,
                                    EventType.Copied);

Now I have to replace the authentication mechanism to use the OAuth protocol. I saw a few examples, but they all seem to be talking about client authentication ( https://msdn.microsoft.com/en-us/library/office/dn903761%28v=exchg.150%29.aspx?f=255&MSPPError= -2147217396 ), but nowhere I could not find an example how to authenticate an Exchange user with OAuth protocol. Any sample code will help a lot. Thank.

+4
2

, "-" . - -, , OAuth2 , , . OAuth2 ( ), ( ), , ExchangeService .

"-" - , , , .

AccessToken AuthenticationContext

, , AuthenticationContext.

, AcquireToken AcquireTokenAsync/AcquireTokenSilentAsync.

, , , . AcquireToken[Async] . , , , . , (, ).

. , :

: , . AuthenticationContext -, . F.e. , (. ).

AccessToken

, . " " , OAuth2 specificatioin/RFC6749.

, , oauth2-client-handler, HttpClient, , , , .

, MSDN, f.e.:

var service = new ExchangeService(exchangeVersion)
                  {
                      KeepAlive = true,
                      Url = new Uri("some autodiscovery url"),
                      Credentials = new OAuthCredentials(authenticationResult.AccessToken))
                  };
+3

Source: https://habr.com/ru/post/1682664/


All Articles