Skip to content

Commit 2911dd7

Browse files
Merge pull request #40 from StuartFerguson/bug/#39_dbcontextusage
Fix Db Context usage issue
2 parents 7ce9a2b + a1d80c9 commit 2911dd7

2 files changed

Lines changed: 80 additions & 87 deletions

File tree

SecurityService.Manager/SecurityServiceManager.cs

Lines changed: 77 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -100,36 +100,34 @@ public async Task<String> CreateApiResource(String name,
100100
List<String> userClaims,
101101
CancellationToken cancellationToken)
102102
{
103-
using(IConfigurationDbContext context = this.ConfigurationDbContextResolver())
103+
IConfigurationDbContext context = this.ConfigurationDbContextResolver();
104+
ApiResource apiResource = new ApiResource
105+
{
106+
ApiSecrets = new List<Secret>
107+
{
108+
new Secret(secret.ToSha256())
109+
},
110+
Description = description,
111+
DisplayName = displayName,
112+
Name = name,
113+
UserClaims = userClaims
114+
};
115+
116+
if (scopes != null && scopes.Any())
104117
{
105-
ApiResource apiResource = new ApiResource
106-
{
107-
ApiSecrets = new List<Secret>
108-
{
109-
new Secret(secret.ToSha256())
110-
},
111-
Description = description,
112-
DisplayName = displayName,
113-
Name = name,
114-
UserClaims = userClaims
115-
};
116-
117-
if (scopes != null && scopes.Any())
118+
foreach (String scope in scopes)
118119
{
119-
foreach (String scope in scopes)
120-
{
121-
apiResource.Scopes.Add(new Scope(scope));
122-
}
120+
apiResource.Scopes.Add(new Scope(scope));
123121
}
122+
}
124123

125-
// Now translate the model to the entity
126-
await context.ApiResources.AddAsync(apiResource.ToEntity(), cancellationToken);
124+
// Now translate the model to the entity
125+
await context.ApiResources.AddAsync(apiResource.ToEntity(), cancellationToken);
127126

128-
// Save the changes
129-
await context.SaveChangesAsync();
127+
// Save the changes
128+
await context.SaveChangesAsync();
130129

131-
return name;
132-
}
130+
return name;
133131
}
134132

135133
/// <summary>
@@ -154,28 +152,27 @@ public async Task<String> CreateClient(String clientId,
154152
// Validate the grant types list
155153
this.ValidateGrantTypes(allowedGrantTypes);
156154

157-
using(IConfigurationDbContext context = this.ConfigurationDbContextResolver())
158-
{
159-
// Create the model from the request
160-
Client client = new Client
155+
IConfigurationDbContext context = this.ConfigurationDbContextResolver();
156+
157+
// Create the model from the request
158+
Client client = new Client
159+
{
160+
ClientId = clientId,
161+
ClientName = clientName,
162+
Description = clientDescription,
163+
ClientSecrets =
161164
{
162-
ClientId = clientId,
163-
ClientName = clientName,
164-
Description = clientDescription,
165-
ClientSecrets =
166-
{
167-
new Secret(secret.ToSha256())
168-
},
169-
AllowedGrantTypes = allowedGrantTypes,
170-
AllowedScopes = allowedScopes
171-
};
172-
173-
// Now translate the model to the entity
174-
await context.Clients.AddAsync(client.ToEntity(), cancellationToken);
175-
176-
// Save the changes
177-
await context.SaveChangesAsync();
178-
}
165+
new Secret(secret.ToSha256())
166+
},
167+
AllowedGrantTypes = allowedGrantTypes,
168+
AllowedScopes = allowedScopes
169+
};
170+
171+
// Now translate the model to the entity
172+
await context.Clients.AddAsync(client.ToEntity(), cancellationToken);
173+
174+
// Save the changes
175+
await context.SaveChangesAsync();
179176

180177
return clientId;
181178
}
@@ -376,20 +373,19 @@ public async Task<ApiResource> GetApiResource(String apiResourceName,
376373
{
377374
ApiResource apiResourceModel = null;
378375

379-
using(IConfigurationDbContext context = this.ConfigurationDbContextResolver())
380-
{
381-
IdentityServer4.EntityFramework.Entities.ApiResource apiResourceEntity = await context.ApiResources.Where(a => a.Name == apiResourceName)
382-
.Include(a => a.Scopes).Include(a => a.UserClaims)
383-
.SingleOrDefaultAsync(cancellationToken:cancellationToken);
376+
IConfigurationDbContext context = this.ConfigurationDbContextResolver();
384377

385-
if (apiResourceEntity == null)
386-
{
387-
throw new NotFoundException($"No Api Resource found with Name [{apiResourceName}]");
388-
}
378+
IdentityServer4.EntityFramework.Entities.ApiResource apiResourceEntity = await context.ApiResources.Where(a => a.Name == apiResourceName)
379+
.Include(a => a.Scopes).Include(a => a.UserClaims)
380+
.SingleOrDefaultAsync(cancellationToken:cancellationToken);
389381

390-
apiResourceModel = apiResourceEntity.ToModel();
382+
if (apiResourceEntity == null)
383+
{
384+
throw new NotFoundException($"No Api Resource found with Name [{apiResourceName}]");
391385
}
392386

387+
apiResourceModel = apiResourceEntity.ToModel();
388+
393389
return apiResourceModel;
394390
}
395391

@@ -401,17 +397,16 @@ public async Task<ApiResource> GetApiResource(String apiResourceName,
401397
public async Task<List<ApiResource>> GetApiResources(CancellationToken cancellationToken)
402398
{
403399
List<ApiResource> apiResourceModels = new List<ApiResource>();
404-
using(IConfigurationDbContext context = this.ConfigurationDbContextResolver())
405-
{
406-
List<IdentityServer4.EntityFramework.Entities.ApiResource> apiResourceEntities =
407-
await context.ApiResources.Include(a => a.Scopes).Include(a => a.UserClaims).ToListAsync(cancellationToken:cancellationToken);
400+
IConfigurationDbContext context = this.ConfigurationDbContextResolver();
408401

409-
if (apiResourceEntities.Any())
402+
List<IdentityServer4.EntityFramework.Entities.ApiResource> apiResourceEntities =
403+
await context.ApiResources.Include(a => a.Scopes).Include(a => a.UserClaims).ToListAsync(cancellationToken:cancellationToken);
404+
405+
if (apiResourceEntities.Any())
406+
{
407+
foreach (IdentityServer4.EntityFramework.Entities.ApiResource apiResourceEntity in apiResourceEntities)
410408
{
411-
foreach (IdentityServer4.EntityFramework.Entities.ApiResource apiResourceEntity in apiResourceEntities)
412-
{
413-
apiResourceModels.Add(apiResourceEntity.ToModel());
414-
}
409+
apiResourceModels.Add(apiResourceEntity.ToModel());
415410
}
416411
}
417412

@@ -430,21 +425,20 @@ public async Task<Client> GetClient(String clientId,
430425
{
431426
Client clientModel = null;
432427

433-
using(IConfigurationDbContext context = this.ConfigurationDbContextResolver())
434-
{
435-
IdentityServer4.EntityFramework.Entities.Client clientEntity = await context
436-
.Clients.Include(c => c.AllowedGrantTypes).Include(c => c.AllowedScopes)
437-
.Where(c => c.ClientId == clientId)
438-
.SingleOrDefaultAsync(cancellationToken:cancellationToken);
428+
IConfigurationDbContext context = this.ConfigurationDbContextResolver();
439429

440-
if (clientEntity == null)
441-
{
442-
throw new NotFoundException($"No client found with Client Id [{clientId}]");
443-
}
430+
IdentityServer4.EntityFramework.Entities.Client clientEntity = await context
431+
.Clients.Include(c => c.AllowedGrantTypes).Include(c => c.AllowedScopes)
432+
.Where(c => c.ClientId == clientId)
433+
.SingleOrDefaultAsync(cancellationToken:cancellationToken);
444434

445-
clientModel = clientEntity.ToModel();
435+
if (clientEntity == null)
436+
{
437+
throw new NotFoundException($"No client found with Client Id [{clientId}]");
446438
}
447439

440+
clientModel = clientEntity.ToModel();
441+
448442
return clientModel;
449443
}
450444

@@ -456,17 +450,16 @@ public async Task<Client> GetClient(String clientId,
456450
public async Task<List<Client>> GetClients(CancellationToken cancellationToken)
457451
{
458452
List<Client> clientModels = new List<Client>();
459-
using(IConfigurationDbContext context = this.ConfigurationDbContextResolver())
460-
{
461-
List<IdentityServer4.EntityFramework.Entities.Client> clientEntities =
462-
await context.Clients.Include(c => c.AllowedGrantTypes).Include(c => c.AllowedScopes).ToListAsync(cancellationToken:cancellationToken);
453+
IConfigurationDbContext context = this.ConfigurationDbContextResolver();
454+
455+
List<IdentityServer4.EntityFramework.Entities.Client> clientEntities =
456+
await context.Clients.Include(c => c.AllowedGrantTypes).Include(c => c.AllowedScopes).ToListAsync(cancellationToken:cancellationToken);
463457

464-
if (clientEntities.Any())
458+
if (clientEntities.Any())
459+
{
460+
foreach (IdentityServer4.EntityFramework.Entities.Client clientEntity in clientEntities)
465461
{
466-
foreach (IdentityServer4.EntityFramework.Entities.Client clientEntity in clientEntities)
467-
{
468-
clientModels.Add(clientEntity.ToModel());
469-
}
462+
clientModels.Add(clientEntity.ToModel());
470463
}
471464
}
472465

SecurityService/appsettings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"ConnectionStrings": {
3-
"PersistedGrantDbContext": "server=localhost;database=PersistedGrantStore;user id=sa;password=sp1ttal",
4-
"ConfigurationDbContext": "server=localhost;database=Configuration;user id=sa;password=sp1ttal",
5-
"AuthenticationDbContext": "server=localhost;database=Authentication;user id=sa;password=sp1ttal"
3+
"PersistedGrantDbContext": "server=192.168.1.133;database=PersistedGrantStore;user id=sa;password=Sc0tland",
4+
"ConfigurationDbContext": "server=192.168.1.133;database=Configuration;user id=sa;password=Sc0tland",
5+
"AuthenticationDbContext": "server=192.168.1.133;database=Authentication;user id=sa;password=Sc0tland"
66
},
77
"SeedingType": "Production",
88
"IdentityOptions": {

0 commit comments

Comments
 (0)