因此,我们有一个带有IdentityServer4核心的.net设置,在一个实例中,一切都如预期的那样工作,但是当我们决定旋转更多的Identity服务器实例时,我们在从客户端登录或退出时随机出现问题。
我跟踪了这些文档:分布式IdentityServer
我就是这样添加IDS4的
_identityBuilder = services.AddIdentityServer(options =>
{
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;
options.EmitStaticAudienceClaim = true;
})
.AddInMemoryIdentityResources(Config.IdentityResources)
.AddInMemoryApiResources(Configuration.GetSection("idServer:apiResources"))
.AddInMemoryApiScopes(Configuration.GetSection("idServer:apiScopes"))
.AddInMemoryClients(Configuration.GetSection("idServer:clients"))
.AddAspNetIdentity<HeimdallUserEntity>()
;另外,由于服务器将被分发,我还添加了这段代码,请注意下面的证书是在实例之间共享的(因此每个实例都使用相同的证书)。
_identityBuilder.AddSigningCredential(certificate);
services.AddDataProtection()
.SetApplicationName(Assembly.GetExecutingAssembly().FullName)
.PersistKeysToDbContext<MainDbContext>()
.ProtectKeysWithCertificate(certificate);然而,即使使用此设置,我在从使用PKCE的客户端登录和输出时也会出现问题(随机)。我遇到的问题是,我随机地得到了这个例外:
HttpContext must not be null.来自:Microsoft.AspNetCore.Identity -> SignInManager -> SignOutAsync()和Microsoft.AspNetCore.Identity -> SignInManager -> SignInWithClaimsAsync(TUser user,AuthenticationProperties authenticationProperties,IEnumerable authenticationProperties)
此异常将在下面的SignInManager.cs类中处理和抛出:
public HttpContext Context
{
get
{
var context = _context ?? _contextAccessor?.HttpContext;
if (context == null)
{
throw new InvalidOperationException("HttpContext must not be null.");
}
return context;
}
set
{
_context = value;
}
}还请注意,client_credentials正常工作,我请求一个令牌,多个实例/副本可以正常工作。
*最新情况:
在我们的系统中,我终于发现了一个与SignInManager,Identity Server无关的问题,在我们的系统中,我们使用Microsoft Orleans,并且我们有一个注入UserService的谷物,并且,UserServiceE 222注入了SignInManager结果表明SignInManager要求E 127HttpContextE 228能够解析服务,但是由于Or良没有提供IHttpContextAccessor,,E 131HttpContextE 232是无法解决的:/ /
现在,我们直接调用UserService。但是,能够创建/找到一个不依赖于SignInManager的HttpContext (特别是因为它只使用它来解析其他服务)是件好事。
发布于 2020-11-19 16:49:54
您是否在不同实例之间使用相同的令牌签名凭据,并使用AddSigningCredential方法添加它们?
这里不认为这是核心问题,但是当您使用PersistKeysToDbContext并在服务之间共享它时,您应该意识到这一点,当多个服务试图写入数据库中的同一个表时,也可能会出现争用条件。特别是在启动时(当DB是空的)和键每90天旋转一次。
https://stackoverflow.com/questions/64913298
复制相似问题