我有一台运行Identity的服务器仅用于身份验证,我想记录每个用户登录。我已经阅读了Identity doc并尝试使用IEventSink
。它应该很容易,但是Login在不调用EventSink的情况下继续工作。我必须在某个地方注册我的班级吗?我错过了什么?
var builder = services
.AddIdentityServer(options =>
{
options.Events.RaiseSuccessEvents = true;
}
)
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApis())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<ApplicationUser>()
.AddOperationalStore(options =>
{
options.ConfigureDbContext = b =>
b.UseMySql(Configuration.GetConnectionString("DefaultConnection")
);
options.EnableTokenCleanup = true;
});
这是我创建的EventSink:
public class MyEventSink : IEventSink
{
public Task PersistAsync(Event evt)
{
if (evt.Id.Equals(EventIds.TokenIssuedSuccess))
{
var _test = evt as TokenIssuedSuccessEvent;
}
throw new System.NotImplementedException(); // shouldn't even login
return Task.CompletedTask;
}
}
正如@ camilo-terevinto向我指出的那样,我只是没有注册我的MyEventSink
作为服务。
我在启动时需要以下行,所以当我设置Events.RaiseSuccessEvents = true
时,IdentityServer知道它是我的服务,它应该调用:
services.AddScoped<IEventSink , MyEventSink>();