-
Notifications
You must be signed in to change notification settings - Fork 1k
[Do not merge until MSAL.NET 4.0 releases] Jmprieur/msal4.0 #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
36e1ced
a6c957b
2f0b69d
473ddfc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Identity.Web.Client.TokenCacheProviders | ||
{ | ||
|
@@ -46,7 +47,12 @@ public class MSALAppSessionTokenCacheProvider : IMSALAppTokenCacheProvider | |
/// <summary> | ||
/// The HTTP context being used by this app | ||
/// </summary> | ||
internal HttpContext HttpContext = null; | ||
internal HttpContext HttpContext { get { return httpContextAccessor.HttpContext; } } | ||
|
||
/// <summary> | ||
/// HTTP context accessor | ||
/// </summary> | ||
internal IHttpContextAccessor httpContextAccessor; | ||
|
||
/// <summary> | ||
/// The duration till the tokens are kept in memory cache. In production, a higher value , upto 90 days is recommended. | ||
|
@@ -63,26 +69,26 @@ public class MSALAppSessionTokenCacheProvider : IMSALAppTokenCacheProvider | |
/// <summary>Initializes a new instance of the <see cref="MSALAppSessionTokenCacheProvider"/> class.</summary> | ||
/// <param name="azureAdOptionsAccessor">The azure ad options accessor.</param> | ||
/// <exception cref="ArgumentNullException">AzureADOptions - The app token cache needs {nameof(AzureADOptions)}</exception> | ||
public MSALAppSessionTokenCacheProvider(IOptionsMonitor<AzureADOptions> azureAdOptionsAccessor) | ||
public MSALAppSessionTokenCacheProvider(IOptionsMonitor<AzureADOptions> azureAdOptionsAccessor, IHttpContextAccessor httpContextAccessor) | ||
{ | ||
this.httpContextAccessor = httpContextAccessor; | ||
if (azureAdOptionsAccessor.CurrentValue == null && string.IsNullOrWhiteSpace(azureAdOptionsAccessor.CurrentValue.ClientId)) | ||
{ | ||
throw new ArgumentNullException(nameof(AzureADOptions), $"The app token cache needs {nameof(AzureADOptions)}, populated with clientId to initialize."); | ||
} | ||
|
||
this.AppId = azureAdOptionsAccessor.CurrentValue.ClientId; | ||
AppId = azureAdOptionsAccessor.CurrentValue.ClientId; | ||
} | ||
|
||
/// <summary>Initializes this instance of TokenCacheProvider with essentials to initialize themselves.</summary> | ||
/// <param name="tokenCache">The token cache instance of MSAL application</param> | ||
/// <param name="httpcontext">The Httpcontext whose Session will be used for caching.This is required by some providers.</param> | ||
public void Initialize(ITokenCache tokenCache, HttpContext httpcontext) | ||
{ | ||
this.AppCacheId = this.AppId + "_AppTokenCache"; | ||
this.HttpContext = httpcontext; | ||
AppCacheId = this.AppId + "_AppTokenCache"; | ||
|
||
tokenCache.SetBeforeAccess(this.AppTokenCacheBeforeAccessNotification); | ||
tokenCache.SetAfterAccess(this.AppTokenCacheAfterAccessNotification); | ||
tokenCache.SetBeforeAccessAsync(this.AppTokenCacheBeforeAccessNotificationAsync); | ||
tokenCache.SetAfterAccessAsync(this.AppTokenCacheAfterAccessNotificationAsync); | ||
tokenCache.SetBeforeWrite(this.AppTokenCacheBeforeWriteNotification); | ||
} | ||
|
||
|
@@ -119,9 +125,9 @@ public void Clear() | |
/// Triggered right before MSAL needs to access the cache. Reload the cache from the persistence store in case it changed since the last access. | ||
/// </summary> | ||
/// <param name="args">Contains parameters used by the MSAL call accessing the cache.</param> | ||
private void AppTokenCacheBeforeAccessNotification(TokenCacheNotificationArgs args) | ||
private async Task AppTokenCacheBeforeAccessNotificationAsync(TokenCacheNotificationArgs args) | ||
{ | ||
this.HttpContext.Session.LoadAsync().Wait(); | ||
await this.HttpContext.Session.LoadAsync(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And we don't need to block any longer! |
||
|
||
SessionLock.EnterReadLock(); | ||
try | ||
|
@@ -147,7 +153,7 @@ private void AppTokenCacheBeforeAccessNotification(TokenCacheNotificationArgs ar | |
/// Triggered right after MSAL accessed the cache. | ||
/// </summary> | ||
/// <param name="args">Contains parameters used by the MSAL call accessing the cache.</param> | ||
private void AppTokenCacheAfterAccessNotification(TokenCacheNotificationArgs args) | ||
private async Task AppTokenCacheAfterAccessNotificationAsync(TokenCacheNotificationArgs args) | ||
{ | ||
// if the access operation resulted in a cache update | ||
if (args.HasStateChanged) | ||
|
@@ -159,8 +165,8 @@ private void AppTokenCacheAfterAccessNotification(TokenCacheNotificationArgs arg | |
|
||
// Reflect changes in the persistent store | ||
byte[] blob = args.TokenCache.SerializeMsalV3(); | ||
this.HttpContext.Session.Set(this.AppCacheId, blob); | ||
this.HttpContext.Session.CommitAsync().Wait(); | ||
HttpContext.Session.Set(this.AppCacheId, blob); | ||
await HttpContext.Session.CommitAsync(); | ||
} | ||
finally | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,12 +52,13 @@ public static IServiceCollection AddSqlAppTokenCache(this IServiceCollection ser | |
{ | ||
// Uncomment the following lines to create the database. In production scenarios, the database | ||
// will most probably be already present. | ||
//var tokenCacheDbContextBuilder = new DbContextOptionsBuilder<TokenCacheDbContext>(); | ||
//tokenCacheDbContextBuilder.UseSqlServer(sqlTokenCacheOptions.SqlConnectionString); | ||
|
||
//var tokenCacheDbContext = new TokenCacheDbContext(tokenCacheDbContextBuilder.Options); | ||
//tokenCacheDbContext.Database.EnsureCreated(); | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Transforming in /* comments so that it's obvious what to uncomment |
||
var tokenCacheDbContextBuilder = new DbContextOptionsBuilder<TokenCacheDbContext>(); | ||
tokenCacheDbContextBuilder.UseSqlServer(sqlTokenCacheOptions.SqlConnectionString); | ||
|
||
var tokenCacheDbContextForCreation = new TokenCacheDbContext(tokenCacheDbContextBuilder.Options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed tokenCacheDbContext -> tokenCacheDbContextForCreation as there was a conflict |
||
tokenCacheDbContextForCreation.Database.EnsureCreated(); | ||
*/ | ||
services.AddDataProtection(); | ||
|
||
services.AddDbContext<TokenCacheDbContext>(options => | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And therefore the callback are async