Error: Scope IdentityServerApi not found in store

Viewed 298

While trying to add routing to additional endpoints within my project built with IdentityServer4, I ran into the following issue:

Error: Scope IdentityServerApi not found in store.

I followed IdentityServer4's documentation for adding more API Endpoints exactly, so I wasn't sure why I was getting this error.

1 Answers

It turns out that in v4 IdentityServer is moving away from ApiResources and toward ApiScopes. For anyone just learning about IdentityServer4, it can be a little confusing navigating all of these changes, so hopefully this clarification will be helpful to someone else as well.

Solution: as opposed to what it says in the documentation included in my question, register your "LocalApi" as an ApiScope not an ApiResource. Following in an example of my setup for a local API:

Your config file:

// Add to your ApiScopes (not ApiResources)
public static IEnumerable<ApiScope> ApiScopes =>
   new ApiScope[]
   {
      new ApiScope(IdentityServerConstants.LocalApi.ScopeName)
   };

// Then add to your client's allowed scopes:
// ... extraneous code omitted ...
AllowedScopes = new string[]
{
   IdentityServerConstants.LocalApi.ScopeName
}

Your startup/program.cs:

// Register IdentityServer, then:
builder.Services.AddLocalApiAuthentication();

Your "LocalApi" controller class:

[ApiController]
[Authorize(LocalApi.PolicyName)]
[Route("api/[controller]/[action]")]
public class YourController : ControllerBase
{
   // ...
}

Related