Skip to content

Commit

Permalink
Added code for custom setting of the scope to be used. This has not b…
Browse files Browse the repository at this point in the history
…een tested yet.
  • Loading branch information
dseph committed Feb 22, 2023
1 parent d5552c9 commit bfd8c78
Show file tree
Hide file tree
Showing 5 changed files with 899 additions and 814 deletions.
20 changes: 0 additions & 20 deletions EWSEditor/Common/Auth/AuthenticationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,8 @@ public class AuthenticationHelper
return Do_OAuth(ref MailboxBeingAccessed, ref AccountAccessingMailbox,
sAuthority, sAppId, sRedirectURL, sServername, ref sBearerToken, PromptBehavior.Always);

//ExchangeCredentials oExchangeCredentials = null;

//// See // https://msdn.microsoft.com/en-us/library/office/dn903761(v=exchg.150).aspx
//// get authentication token
//string authority = sAuthority;
//string clientID = sAppId;
//Uri clientAppUri = new Uri(sRedirectURL);
//string serverName = sServername;

//AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);
//PlatformParameters oPlatformParameters = new PlatformParameters(PromptBehavior.Always);
//AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync (serverName, clientID, clientAppUri, oPlatformParameters).Result;

//sBearerToken = authenticationResult.AccessToken;

//// Add authenticaiton token to requests
//oExchangeCredentials = new OAuthCredentials(authenticationResult.AccessToken);

//MailboxBeingAccessed = authenticationResult.UserInfo.DisplayableId;
//AccountAccessingMailbox = authenticationResult.UserInfo.DisplayableId; // oAuth at this time does not support delegate or impersonation access - may need to change this in the future.

// return oExchangeCredentials;

}

Expand Down
82 changes: 54 additions & 28 deletions EWSEditor/Common/Auth/oAuthHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Net.NetworkInformation;

// For reference:
// https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-client-application-configuration
Expand Down Expand Up @@ -62,17 +63,30 @@ public PublicClientApplication CurrentPublicClientApplication
}




///------------------------------------------------------------------------------------------------------
/// <summary>
///
/// </summary>
/// <param name="ClientId"></param>
/// <param name="TenantId"></param>
/// <param name="OAuth2RedirectUrl"></param>
/// <param name="OAuth2Authority"></param>
/// <param name="OAuth2ValidateAuthority"></param>
/// <returns></returns>
public async Task<AuthenticationResult> GetDelegateToken(
string ClientId,
string TenantId,
string OAuth2RedirectUrl,
string OAuth2Authority,
bool OAuth2ValidateAuthority)
bool OAuth2ValidateAuthority,
string OAuth2Scope
)
{
_Success = false;

//var ewsScopes = new string[] { "https://outlook.office365.com/EWS.AccessAsUser.All" };
string[] ewsScopes = { OAuth2Scope };

// Using Microsoft.Identity.Client 4.22.0
PublicClientApplicationOptions pcaOptions = null;

Expand Down Expand Up @@ -105,7 +119,7 @@ public PublicClientApplication CurrentPublicClientApplication
.CreateWithApplicationOptions(pcaOptions).Build();

// The permission scope required for EWS access
var ewsScopes = new string[] { "https://outlook.office365.com/EWS.AccessAsUser.All" };

AuthenticationResult oResult = null;

Expand All @@ -131,21 +145,34 @@ public PublicClientApplication CurrentPublicClientApplication
return null;

}




// ------------------------------------------------------------------------------------------------------
/// <summary>
///
/// </summary>
/// <param name="ClientId"></param>
/// <param name="TenantId"></param>
/// <param name="ClientSecret"></param>
/// <param name="OAuth2RedirectUrl"></param>
/// <param name="OAuth2Authority"></param>
/// <param name="OAuth2ValidateAuthority"></param>
/// <param name="OAuth2Scope"></param>
/// <returns></returns>
public async Task<AuthenticationResult> GetApplicationToken(
string ClientId,
string TenantId,
string ClientSecret,
string OAuth2RedirectUrl,
string OAuth2Authority,
bool OAuth2ValidateAuthority)
bool OAuth2ValidateAuthority,
string OAuth2Scope )
{

// Configure the MSAL client to get tokens
var ewsScopes = new string[] { "https://outlook.office.com/.default" };
//var ewsScopes = new string[] { "https://outlook.office.com/.default" };
string[] ewsScopes = { OAuth2Scope };

IConfidentialClientApplication app = null;

try
Expand Down Expand Up @@ -215,16 +242,31 @@ public PublicClientApplication CurrentPublicClientApplication

}

// ----------------------------------------------------------------------
/// <summary>
///
/// </summary>
/// <param name="ClientId"></param>
/// <param name="TenantId"></param>
/// <param name="ClientCertificate"></param>
/// <param name="OAuth2RedirectUrl"></param>
/// <param name="OAuth2Authority"></param>
/// <param name="OAuth2ValidateAuthority"></param>
/// <param name="OAuth2Scope"></param>
/// <returns></returns>
public async Task<AuthenticationResult> GetCertificateToken(
string ClientId,
string TenantId,
X509Certificate2 ClientCertificate,
string OAuth2RedirectUrl,
string OAuth2Authority,
bool OAuth2ValidateAuthority)
bool OAuth2ValidateAuthority,
string OAuth2Scope)
{
// Configure the MSAL client to get tokens
var ewsScopes = new string[] { "https://outlook.office.com/.default" };
//var ewsScopes = new string[] { "https://outlook.office.com/.default" };
string[] ewsScopes = { OAuth2Scope };


IConfidentialClientApplication app = null;

Expand Down Expand Up @@ -276,25 +318,9 @@ public async Task<AuthenticationResult> GetRefreshToken(PublicClientApplication
{

// https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-error-handling-dotnet#msaluirequiredexception


//var pcaOptions = new PublicClientApplicationOptions
//{
// ClientId = sClientId,
// TenantId = sTenantId
//};



//var pca = PublicClientApplicationBuilder
// .CreateWithApplicationOptions(pcaOptions).Build();


// https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/AcquireTokenSilentAsync-using-a-cached-token

//var accounts = await pca.GetAccountsAsync();
//var firstAccount = accounts.FirstOrDefault();




var accounts = await oPCA.GetAccountsAsync();

Expand Down
1 change: 1 addition & 0 deletions EWSEditor/Common/EwsProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class EwsProxyFactory
public static string OAuth2RedirectUrl = string.Empty;
public static string OAuth2Authority = string.Empty;
public static bool OAuth2ValidateAuthority = true;
public static string OAuth2Scope = string.Empty;

public static bool? UseOAuthDelegate = null;
public static bool? UseOAuthApplication = null;
Expand Down

0 comments on commit bfd8c78

Please sign in to comment.