Reputation: 163
Hello
Today i'm starting with IdentityServer4, i would like to start a ASP.NET Core 2.2 Site with IdentityServer4 and i ConsoleClient (later an Xamarin Client). But i am desperate it doesnt work. Hope someone can help me.
I get the following error when i try to call a web api controller from the ConsoleClient to the ASP.NET site.
Best regards
[02:50:17 Information] Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler
Failed to validate the token.
Microsoft.IdentityModel.Tokens.SecurityTokenInvalidAudienceException: IDX10214: Audience validation failed. Audiences: '[PII is hidden]'. Did not match: validationParameters.ValidAudience: '[PII is hidden]' or validationParameters.ValidAudiences: '[PII is hidden]'.
at Microsoft.IdentityModel.Tokens.Validators.ValidateAudience(IEnumerable`1 audiences, SecurityToken securityToken, TokenValidationParameters validationParameters) in C:\agent2\_work\56\s\src\Microsoft.IdentityModel.Tokens\Validators.cs:line 108
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateTokenPayload(JwtSecurityToken jwtToken, TokenValidationParameters validationParameters) in C:\agent2\_work\56\s\src\System.IdentityModel.Tokens.Jwt\JwtSecurityTokenHandler.cs:line 737
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(String token, TokenValidationParameters validationParameters, SecurityToken& validatedToken) in C:\agent2\_work\56\s\src\System.IdentityModel.Tokens.Jwt\JwtSecurityTokenHandler.cs:line 719
at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
[02:50:17 Information] Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler
BearerIdentityServerAuthenticationJwt was not authenticated. Failure message: IDX10214: Audience validation failed. Audiences: '[PII is hidden]'. Did not match: validationParameters.ValidAudience: '[PII is hidden]' or validationParameters.ValidAudiences: '[PII is hidden]'.
[02:50:17 Information] IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationHandler
Bearer was not authenticated. Failure message: IDX10214: Audience validation failed. Audiences: '[PII is hidden]'. Did not match: validationParameters.ValidAudience: '[PII is hidden]' or validationParameters.ValidAudiences: '[PII is hidden]'.
[02:50:17 Information] Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler
AuthenticationScheme: BearerIdentityServerAuthenticationJwt was challenged.
[02:50:17 Information] IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationHandler
AuthenticationScheme: Bearer was challenged.
Here is my Config.cs:
namespace IdentityServer
{
public static class Config
{
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new IdentityResource[]
{
new IdentityResources.OpenId()
};
}
public static List<TestUser> GetUsers()
{
return new List<TestUser>
{
new TestUser
{
SubjectId = "1",
Username = "alice",
Password = "password"
},
new TestUser
{
SubjectId = "2",
Username = "bob",
Password = "bob"
}
};
}
public static IEnumerable<ApiResource> GetApis()
{
return new List<ApiResource>
{
new ApiResource("api1", "My API")
};
}
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "client",
RequireConsent = false,
AllowAccessTokensViaBrowser = true,
// no interactive user, use the clientid/secret for authentication
AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
// secret for authentication
ClientSecrets =
{
new Secret("secret".Sha256())
},
// scopes that client has access to
AllowedScopes = { "api1" }
}
};
}
}
}
My Startup.cs:
public class Startup
{
public IHostingEnvironment Environment { get; }
public Startup(IHostingEnvironment environment)
{
Environment = environment;
}
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvcCore()
.AddJsonFormatters()
.AddAuthorization();
services.AddIdentityServer(options =>
{
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;
options.IssuerUri = "http://localhost:5000";
}
)
.AddInMemoryClients(Config.GetClients())
.AddInMemoryApiResources(Config.GetApis())
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddDeveloperSigningCredential()
.AddJwtBearerClientAuthentication()
.AddTestUsers(Config.GetUsers());
services.AddAuthentication()
.AddIdentityServerAuthentication(o =>
{
o.Authority = "http://localhost:5000";
o.ApiName = "client";
o.ApiSecret = "secret";
o.EnableCaching = true;
o.RequireHttpsMetadata = false;
o.SaveToken = true;
}).AddCookie();
services.AddCors();
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseCors(policy =>
{
policy.WithOrigins(
"http://localhost:5000",
"http://localhost:5001");
policy.AllowAnyHeader();
policy.AllowAnyMethod();
policy.WithExposedHeaders("WWW-Authenticate");
});
if (Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseIdentityServer();
app.UseMvcWithDefaultRoute();
}
}
Sample Auth. Controller:
[Route("api/identity")]
[Authorize(AuthenticationSchemes = IdentityServerAuthenticationDefaults.AuthenticationScheme)]
public class IdentityController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
//return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
return new OkObjectResult("OK");
}
}
Sample Client Console App:
class Program
{
static HttpClient _tokenClient = new HttpClient();
static DiscoveryCache _cache = new DiscoveryCache("http://localhost:5000");
static async Task Main()
{
Console.Title = "Console ResourceOwner Flow UserInfo";
var response = await RequestTokenAsync();
response.Show();
await CallServiceAsync(response.AccessToken);
}
static async Task CallServiceAsync(string token)
{
var baseAddress = "http://localhost:5000";
var client = new HttpClient
{
BaseAddress = new Uri(baseAddress)
};
client.SetBearerToken(token);
var response = await client.GetStringAsync("api/identity");
}
static async Task<TokenResponse> RequestTokenAsync()
{
var disco = await _cache.GetAsync();
if (disco.IsError) throw new Exception(disco.Error);
var response = await _tokenClient.RequestPasswordTokenAsync(new PasswordTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = "client",
ClientSecret = "secret",
UserName = "bob",
Password = "bob",
Scope = "api1"
});
if (response.IsError) throw new Exception(response.Error);
return response;
}
}
Upvotes: 4
Views: 10252
Reputation: 4802
In your startup.cs.
you would need to set your ApiName
to api1
based on your ApiResource
config:
.AddIdentityServerAuthentication(o =>
{
...
o.ApiName = "api1";
...
}).AddCookie();
Upvotes: 1