记录用户登录以在IdentityServer4上进行审核

问题描述 投票:0回答:1

我有一台运行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;
    }
}
c# authentication asp.net-core identityserver4
1个回答
0
投票

正如@ camilo-terevinto向我指出的那样,我只是没有注册我的MyEventSink作为服务。 我在启动时需要以下行,所以当我设置Events.RaiseSuccessEvents = true时,IdentityServer知道它是我的服务,它应该调用:

services.AddScoped<IEventSink , MyEventSink>();

© www.soinside.com 2019 - 2024. All rights reserved.