IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Access ClientScopes in DbContext

Open latchkostov opened this issue 9 years ago • 8 comments

I am building a management UI/API. One of the last pieces remaining is the ability to associate/dissociate a Client with an APIResource. Unfortunately, I don't see the ConfigurationDbContext to have a way for me to access the Scopes so that I can modify a Client to be compatible with more API Resources after creation.

public async Task<bool> ReplaceApiResources(Client client, IEnumerable<ApiResource> apiResources)
{
	var scopeNames = client.AllowedScopes.Select(x => x.Scope).ToArray();

	var currentApiResources = await _context.ApiResources
		.Where(x => scopeNames.Contains(x.Name))
		.ToArrayAsync();

	foreach (var currentResource in currentApiResources)
	{
		if (!apiResources.Contains(currentResource))
		{
			var scope = client.AllowedScopes.FirstOrDefault(x => x.Scope == currentResource.Name);
			client.AllowedScopes.Remove(scope);
		}
	}

	var scopeNamesToAdd = new List<string>();

	foreach (var requestedResource in apiResources)
	{
		if (!currentApiResources.Contains(requestedResource))
		{
			scopeNamesToAdd.Add(requestedResource.Name);
		}
	}

	// Below is where I would like to pull the ClientScopes and add them
	// to Client.AllowedScopes
	foreach (var scopeNameToAdd in scopeNamesToAdd)
	{
	}
}

latchkostov avatar Sep 06 '17 14:09 latchkostov

I'm have this problem too. Do you manage user's tokens?

AlexGnatuyk avatar Sep 06 '17 14:09 AlexGnatuyk

@AlexGnatuyk I don't do anything with the tokens myself. Basically manage users, roles, and have a roles associated to API Resources.

latchkostov avatar Sep 06 '17 15:09 latchkostov

@blgrnboy Ok. One more question. Do you manage users? For example, if you want block specific user.

AlexGnatuyk avatar Sep 06 '17 15:09 AlexGnatuyk

@AlexGnatuyk I have the following tables: Users, Groups, Roles, RoleApiResource, UserRoleApiResource and GroupRoleApiResource. To "block" a user, I end up removing ApiResourceRoles from that user.

latchkostov avatar Sep 06 '17 15:09 latchkostov

@blgrnboy Can I see your code? Maybe something will be useful for me, or is it a private project?

AlexGnatuyk avatar Sep 07 '17 08:09 AlexGnatuyk

Any update on the issue? closing for now - feel free to re-open if it needs further discussion.

leastprivilege avatar Sep 19 '17 05:09 leastprivilege

I also needed to perform the same action.

To do this, i took advantage of AsQueryable() in System.Linq and Include() in the EntityframeworkQueryableExtensions to include the AllowedScopes enties.

Where you're fetching your client from the ConfigurationDbContext, you can do this:

 await _configurationDbContext.Clients
                    .AsQueryable().Include(c => c.AllowedScopes)
                    .FirstOrDefaultAsync(c => c.ClientId.Equals(clientId, StringComparison.OrdinalIgnoreCase));

Then any changes to the AllowedScopes will be stored when you call

```await _configurationDbContext.SaveChangesAsync();`

Darrenthebrit avatar Mar 09 '18 18:03 Darrenthebrit

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 13 '20 15:01 lock[bot]