Access ClientScopes in DbContext
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)
{
}
}
I'm have this problem too. Do you manage user's tokens?
@AlexGnatuyk I don't do anything with the tokens myself. Basically manage users, roles, and have a roles associated to API Resources.
@blgrnboy Ok. One more question. Do you manage users? For example, if you want block specific user.
@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.
@blgrnboy Can I see your code? Maybe something will be useful for me, or is it a private project?
Any update on the issue? closing for now - feel free to re-open if it needs further discussion.
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();`
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.