我试图按照https://aspnetboilerplate.com/Pages/Documents/Zero/Identity-Server中的说明实现IdentityServer,但示例无法正常工作。
我从ASP.NET Boilerplate中开始了一个Core 2.0 Angular项目。是否有基于文档的更新工作示例?
存在多个问题之一是与AuthConfigurer.cs有关。
API调用者(客户端)无法通过令牌验证。
事实上,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);
}
但是在 Startup
类中,AddIdentity
和 AddAuthentication
创建了不同的令牌值和验证规则。
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;
});
令牌可以由双方生成。CreateAccessToken
被Angular客户端和API客户端调用,如下所示:
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");
但是其中一个(根据认证部分)不能通过身份验证。
我需要 API 客户端身份验证和 Angular 客户端身份验证都能够正常工作。
我从以下链接中获取了一些关于双重身份验证的线索:
https://wildermuth.com/2017/08/19/Two-AuthorizationSchemes-in-ASP-NET-Core-2
但我无法解决这个问题。任何评论都非常有价值,以解决这个问题。