简体   繁体   English

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

[英]Record user login for audit on IdentityServer4

I have a server running for Identity only for authentication and I want to log every user login. 我有一台仅为身份验证而运行的服务器,我想记录每个用户的登录信息。 I've read the Identity doc and tried using the IEventSink . 我已经阅读了Identity doc,并尝试使用IEventSink It's supposed to be easy, but Login keeps working without calling the EventSink. 它应该很简单,但是Login可以继续工作而无需调用EventSink。 Do I have to register my class somewhere? 我必须在某个地方注册我的课程吗? What am I missing? 我想念什么?

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;
});

This is the EventSink I created: 这是我创建的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;
    }
}

As @camilo-terevinto pointed out to me, I just wasn't registering my MyEventSink as a service. 正如@ camilo-terevinto向我指出的那样,我只是没有将MyEventSink注册为服务。
I needed the following line in my startup, so when I set Events.RaiseSuccessEvents = true the IdentityServer knows it is my service it should call: 我在启动时需要以下行,因此当我设置Events.RaiseSuccessEvents = true ,IdentityServer知道应该调用它是我的服务:

services.AddScoped<IEventSink , MyEventSink>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM