Forked from aspose-com-gists/EwsConnectionWithAppAuth.cs
Created
December 12, 2023 23:20
-
-
Save uzbekdev1/ddd7ab8a32f4495b2a0ec0ebf4088da7 to your computer and use it in GitHub Desktop.
How To Connect to Microsoft365 Mailbox using Modern Authentication in C# .NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use Microsoft365 username and access token | |
NetworkCredential credentials = new OAuthNetworkCredential(username, accessToken); | |
using var client = EWSClient.GetEWSClient("https://outlook.office365.com/EWS/Exchange.asmx", credentials); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NetworkCredential credentials = new OAuthNetworkCredential(accessToken); | |
using var client = EWSClient.GetEWSClient("https://outlook.office365.com/EWS/Exchange.asmx", credentials); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccessParameters | |
{ | |
public string TenantId { get; set; } | |
public string ClientId { get; set; } | |
public string RedirectUri { get; set; } = "http://localhost"; | |
public string[] Scopes { get; set; } = { | |
"https://outlook.office.com/IMAP.AccessAsUser.All", | |
"https://outlook.office.com/SMTP.Send" }; | |
} | |
public static async Task<string> GetAccessToken(AccessParameters accessParameters) | |
{ | |
var pca = PublicClientApplicationBuilder | |
.Create(accessParameters.ClientId) | |
.WithTenantId(accessParameters.TenantId) | |
.WithRedirectUri(ccessParameters.RedirectUri) | |
.Build(); | |
var result = await pca.AcquireTokenInteractive(accessParameters.Scopes) | |
.WithUseEmbeddedWebView(false) | |
.ExecuteAsync(); | |
return result.AccessToken; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccessParameters | |
{ | |
public string TenantId { get; set; } | |
public string ClientId { get; set; } | |
public string ClientSecret { get; set; } | |
public string[] Scopes { get; set; } = { "https://outlook.office365.com/.default" }; | |
} | |
public static async Task<string> GetAccessToken(AccessParameters accessParameters) | |
{ | |
var cca = ConfidentialClientApplicationBuilder | |
.Create(accessParameters.ClientId) | |
.WithClientSecret(accessParameters.ClientSecret) | |
.WithTenantId(accessParameters.TenantId) | |
.Build(); | |
var result = await cca.AcquireTokenForClient(accessParameters.Scopes).ExecuteAsync(); | |
return result.AccessToken; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccessParameters | |
{ | |
public string TenantId { get; set; } | |
public string ClientId { get; set; } | |
public string RedirectUri { get; set; } = "http://localhost"; | |
public string[] Scopes { get; set; } = { "https://outlook.office365.com/EWS.AccessAsUser.All" }; | |
} | |
public static async Task<string> GetAccessToken(AccessParameters accessParameters) | |
{ | |
var pca = PublicClientApplicationBuilder | |
.Create(accessParameters.ClientId) | |
.WithTenantId(accessParameters.TenantId) | |
.WithRedirectUri(ccessParameters.RedirectUri) | |
.Build(); | |
var result = await pca.AcquireTokenInteractive(accessParameters.Scopes) | |
.WithUseEmbeddedWebView(false) | |
.ExecuteAsync(); | |
return result.AccessToken; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var imapClient = new ImapClient( | |
"outlook.office365.com", | |
993, | |
username, | |
accessToken, | |
true); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var smtpClient = new SmtpClient( | |
"smtp.office365.com", | |
587, | |
username, | |
accessToken, | |
true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment