Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

Commit

Permalink
Updated SqlServerPersistedGrantStore.StoreAsync method so that we che…
Browse files Browse the repository at this point in the history
…ck if the grant exists before adding it.
  • Loading branch information
michael.vidal committed Dec 12, 2017
1 parent 76d691b commit 42f3ed8
Showing 1 changed file with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
using Microsoft.EntityFrameworkCore;
using Fabric.Identity.API.Persistence.SqlServer.EntityModels;
using Fabric.Identity.API.Persistence.SqlServer.Mappers;
using Serilog;


namespace Fabric.Identity.API.Persistence.SqlServer.Stores
{
public class SqlServerPersistedGrantStore : IPersistedGrantStore
{
private readonly IIdentityDbContext _identityDbContext;
private readonly ILogger _logger;

public SqlServerPersistedGrantStore(IIdentityDbContext identityDbContext)
public SqlServerPersistedGrantStore(IIdentityDbContext identityDbContext, ILogger logger)
{
_identityDbContext = identityDbContext;
_logger = logger;
}

public Task<IEnumerable<IdentityServer4.Models.PersistedGrant>> GetAllAsync(string subjectId)
Expand Down Expand Up @@ -64,14 +67,36 @@ public async Task RemoveAsync(string key)
private async Task DeletePersistedGrantsAsync(IQueryable<PersistedGrant> persistedGrants)
{
await persistedGrants.ForEachAsync(pg => _identityDbContext.PersistedGrants.Remove(pg));
await _identityDbContext.SaveChangesAsync();
try
{
await _identityDbContext.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException ex)
{
_logger.Warning("Exception removing persistedGrants from the database. Error: {message}", ex.Message);
}
}

public async Task StoreAsync(IdentityServer4.Models.PersistedGrant grant)
{
var persistedGrantEntity = grant.ToEntity();
_identityDbContext.PersistedGrants.Add(persistedGrantEntity);
await _identityDbContext.SaveChangesAsync();
var existingGrant = _identityDbContext.PersistedGrants.SingleOrDefault(pg => pg.Key == grant.Key);
if (existingGrant == null)
{
var persistedGrantEntity = grant.ToEntity();
_identityDbContext.PersistedGrants.Add(persistedGrantEntity);
}
else
{
grant.ToEntity(existingGrant);
}
try
{
await _identityDbContext.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException ex)
{
_logger.Warning("Exception updating {grantKey}. Error: {error}", grant.Key, ex.Message);
}
}
}
}

0 comments on commit 42f3ed8

Please sign in to comment.