I tried to implement IdentityServer as it is explained at https://aspnetboilerplate.com/Pages/Documents/Zero/Identity-Server
But the sample does not work.
I started a Core 2.0 Angular project from ASP.NET Boilerplate. Is there any updated working sample based on the documentation?
There is more than one problem, but one of them is with AuthConfigurer.cs.
The API caller (client) cannot pass the token validation.
Actually, there is a token generation code in TokenAuthController.cs:
private string CreateAccessToken(IEnumerable<Claim> claims, TimeSpan? expiration = null)
{
var now = DateTime.UtcNow;
var jwtSecurityToken = new JwtSecurityToken(
issuer: _configuration.Issuer,
audience: _configuration.Audience,
claims: claims,
notBefore: now,
expires: now.Add(expiration ?? _configuration.Expiration),
signingCredentials: _configuration.SigningCredentials
);
return new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
}
But in the Startup
class, AddIdentity
and AddAuthentication
create different token values and validation rules.
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
.AddInMemoryApiResources(IdentityServerConfig.GetApiResources())
.AddInMemoryClients(IdentityServerConfig.GetClients())
.AddAbpPersistedGrants<IAbpPersistedGrantDbContext>()
.AddAbpIdentityServer<User>(); ;
services.AddAuthentication().AddIdentityServerAuthentication("IdentityBearer", options =>
{
options.Authority = "http://localhost:62114/";
options.RequireHttpsMetadata = false;
});
The token can be generated by both sides. CreateAccessToken
is called by the Angular client and by the API client as shown below:
var disco = await DiscoveryClient.GetAsync("http://localhost:21021");
var httpHandler = new HttpClientHandler();
httpHandler.CookieContainer.Add(new Uri("http://localhost:21021/"), new Cookie(MultiTenancyConsts.TenantIdResolveKey, "1")); //Set TenantId
var tokenClient = new TokenClient(disco.TokenEndpoint, "AngularSPA", "secret", httpHandler);
var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync("admin", "123qwe", "default-api"); //RequestClientCredentialsAsync("default-api");
But just one of them (according to the Authentication part) cannot pass authentication.
I need both the API client authentication and the Angular client authentication to work.
I have some clue from a link about dual authentication:
https://wildermuth.com/2017/08/19/Two-AuthorizationSchemes-in-ASP-NET-Core-2
But I could not solve this. Any comment is very valuable to solve the problem.