0

I have created an identity server 4 project and a mvc client project. Authentication flow works as expected. I have added an API controller in the same project as identity server and i want to hit this api resource from mvc client.Essentially,i need both identity server middleware and token validation middle inside the idenity server project.

madCode
  • 313
  • 1
  • 3
  • 8

1 Answers1

1

If you haven't already, add these Nuget packages to your already established IdentityServer app/site:

IdentityServer4.AccessTokenValidation
Microsoft.AspNetCore.Mvc

Add another Api Resource to your resources list:

public static IEnumerable<ApiResource> GetApiResources()
{
    return new List<ApiResource>
    {
        new ApiResource("api1", "My API"), 
        new ApiResource("api2", "IdentityServer API")
    };
}

Update your client configuration to allow api2:

public static IEnumerable<Client> GetClients()
{
    return new List<Client>
    {
        new Client
        {
            ClientId = "mvc",

            ... omitted

            AllowedScopes = new List<string>
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile,
                "api2"
            }
        }
    };
}

In the IdentityServer's Configure method in Startup add:

app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
    Authority = "http://localhost:5000",
    RequireHttpsMetadata = false,

    ApiName = "api2"
});
travis.js
  • 5,193
  • 1
  • 24
  • 21
  • I have done something similar. It turns out tht i was goofing up with audience in access token :( . It's working now. Thanks :) – madCode Feb 23 '17 at 11:09
  • Can you add your consuming code to this example from the client. That is the part I am not understanding? – jwize Jun 08 '19 at 18:52