@@ -918,76 +918,92 @@ internal async Task<string> GetOAuth2Token(string tenantId, string clientId, str
918918 string baseAddress = $ "https://login.microsoftonline.com/{ tenantId } /oauth2/token";
919919 string grant_type = "client_credentials" ;
920920
921- bool useMSAL = true ;
922-
923- if ( useMSAL )
924- {
925- var app = ConfidentialClientApplicationBuilder
926- . Create ( clientId )
927- . WithTenantId ( tenantId )
928- . WithClientSecret ( clientSecret )
929- . Build ( ) ;
930-
931- string [ ] scopes = new string [ ] {
932- "https://outlook.office365.com/.default" ,
933- } ;
934-
935- var result = await app . AcquireTokenForClient ( scopes ) . ExecuteAsync ( ) ;
936- string accessToken = result . AccessToken ;
937- return accessToken ;
938- }
939- else
921+ try
940922 {
941- //[dt] token generation is OK but AUTH fails, further investigations needed
942- var client = new HttpClient ( ) ;
923+ bool useMSAL = true ;
943924
944- List < string > scopes = new List < string >
925+ if ( useMSAL )
945926 {
946- "https://outlook.office365.com/.default" ,
947- } ;
948-
949- var form = new Dictionary < string , string >
927+ var app = ConfidentialClientApplicationBuilder
928+ . Create ( clientId )
929+ . WithTenantId ( tenantId )
930+ . WithClientSecret ( clientSecret )
931+ . Build ( ) ;
932+
933+ string [ ] scopes = new string [ ] {
934+ "https://outlook.office365.com/.default" ,
935+ } ;
936+
937+ var result = await app . AcquireTokenForClient ( scopes ) . ExecuteAsync ( ) ;
938+ string accessToken = result . AccessToken ;
939+ return accessToken ;
940+ }
941+ else
950942 {
951- { "grant_type" , grant_type } ,
952- { "client_id" , clientId } ,
953- { "client_secret" , clientSecret } ,
954- { "scope" , string . Join ( " " , scopes ) }
955- } ;
956-
957- HttpResponseMessage tokenResponse = await client . PostAsync ( baseAddress , new FormUrlEncodedContent ( form ) ) ;
958- var jsonContent = await tokenResponse . Content . ReadAsStringAsync ( ) ;
959- OAuth2Token token = JsonConvert . DeserializeObject < OAuth2Token > ( jsonContent ) ;
960- return token . AccessToken ;
943+ //[dt] token generation is OK but AUTH fails, further investigations needed
944+ var client = new HttpClient ( ) ;
945+
946+ List < string > scopes = new List < string >
947+ {
948+ "https://outlook.office365.com/.default" ,
949+ } ;
950+
951+ var form = new Dictionary < string , string >
952+ {
953+ { "grant_type" , grant_type } ,
954+ { "client_id" , clientId } ,
955+ { "client_secret" , clientSecret } ,
956+ { "scope" , string . Join ( " " , scopes ) }
957+ } ;
958+
959+ HttpResponseMessage tokenResponse = await client . PostAsync ( baseAddress , new FormUrlEncodedContent ( form ) ) ;
960+ var jsonContent = await tokenResponse . Content . ReadAsStringAsync ( ) ;
961+ OAuth2Token token = JsonConvert . DeserializeObject < OAuth2Token > ( jsonContent ) ;
962+ return token . AccessToken ;
963+ }
964+ }
965+ catch ( Exception ex )
966+ {
967+ OnAuthenticating ( new AuthenticatingEventArgsBase { Message = $ "error during token generation: { ex . Message } \n { ex . StackTrace } " } ) ;
968+ throw ex ;
961969 }
962970 }
963971
964- public async Task < string > LoginOAuth2 ( string userName , string tenantId , string clientId , string clientSecret )
972+ public async Task < bool > LoginOAuth2 ( string userName , string tenantId , string clientId , string clientSecret )
965973 {
966- var token = await GetOAuth2Token ( tenantId , clientId , clientSecret ) ;
967-
968- OnAuthenticating ( new AuthenticatingOAuth2EventArgs ( userName , host ) ) ;
969-
970- //https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth
971974 string response = "" ;
972- string xOauth2 ;
973- using ( var ms = new MemoryStream ( token . Length + 200 ) )
975+ try
974976 {
975- using ( var bw = new BinaryWriter ( ms ) )
977+ var token = await GetOAuth2Token ( tenantId , clientId , clientSecret ) ;
978+
979+ OnAuthenticating ( new AuthenticatingOAuth2EventArgs ( userName , host ) ) ;
980+ //https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth
981+ string xOauth2 ;
982+ using ( var ms = new MemoryStream ( token . Length + 200 ) )
976983 {
977- bw . Write ( Encoding . ASCII . GetBytes ( "user=" ) ) ;
978- bw . Write ( Encoding . ASCII . GetBytes ( userName ) ) ;
979- bw . Write ( ( byte ) 1 ) ;
980- bw . Write ( Encoding . ASCII . GetBytes ( "auth=Bearer " ) ) ;
981- bw . Write ( Encoding . ASCII . GetBytes ( token ) ) ;
982- bw . Write ( ( byte ) 1 ) ;
983- bw . Write ( ( byte ) 1 ) ;
984+ using ( var bw = new BinaryWriter ( ms ) )
985+ {
986+ bw . Write ( Encoding . ASCII . GetBytes ( "user=" ) ) ;
987+ bw . Write ( Encoding . ASCII . GetBytes ( userName ) ) ;
988+ bw . Write ( ( byte ) 1 ) ;
989+ bw . Write ( Encoding . ASCII . GetBytes ( "auth=Bearer " ) ) ;
990+ bw . Write ( Encoding . ASCII . GetBytes ( token ) ) ;
991+ bw . Write ( ( byte ) 1 ) ;
992+ bw . Write ( ( byte ) 1 ) ;
993+ }
994+ xOauth2 = Convert . ToBase64String ( ms . ToArray ( ) ) ;
984995 }
985- xOauth2 = Convert . ToBase64String ( ms . ToArray ( ) ) ;
996+ var cmd = $ "AUTHENTICATE XOAUTH2 { xOauth2 } ";
997+
998+ response = Command ( cmd ) ;
999+ }
1000+ catch ( Exception ex )
1001+ {
1002+ OnAuthenticating ( new AuthenticatingEventArgsBase { Message = $ "error during token login: { ex . Message } \n { ex . StackTrace } " } ) ;
1003+ return false ;
9861004 }
987- var cmd = $ "AUTHENTICATE XOAUTH2 { xOauth2 } ";
988- response = Command ( cmd ) ;
9891005 OnAuthenticated ( new AuthenticatedOAuth2EventArgs ( userName , host , response ) ) ;
990- return response ;
1006+ return true ;
9911007 }
9921008
9931009
0 commit comments