IdentityServer 4在登录/刷新令牌时获取时间戳

编程入门 行业动态 更新时间:2024-10-26 08:26:28
本文介绍了IdentityServer 4在登录/刷新令牌时获取时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们正在使用身份服务器4 保护api /资源。要求之一是跟踪用户活动,这意味着用户最后一次使用api(未登录但已使用)。由于我们拥有30多种api,我们认为,一旦令牌针对身份服务器。

We are using identity server 4 to protect the api/resources. One of the requirements is to trace the user activity which means, the last time the user consumed the api (not logged in but consumed). As we have 30+ apis, we thought it would be easrier to intercept this validation process/event to register in the database the last activity date once the token gets validated against the identity server.

我的问题是,每次用户想要访问api时,此验证是否真的在身份服务器级别上进行?

My question here, does this validation really happens on identity server level each and every time the user wants to access an api?

是否有获取此验证时间戳的方法以将其保存在数据库中?

Is there anyway to get this validation timestamp to save it somewhere in a database?

谢谢

推荐答案

我自己能够解决此问题。我对ID-Server事件进行了更深入的研究,找到了一种集中处理事件的好方法。因此,此实现仅在ID-Server项目中。

I was able to solve this problem myself. I digged more into ID-Server events and found a good way in handling the events in a centralized way. So this implementation is only in the ID-Server project.

身份服务器4公开了一些可用于跟踪用户活动的事件(例如:令牌成功发行) ,令牌发布失败,登录失败等...)

Identity server 4 exposes some kind of events that can be used to trace user activity (for exmaple: token issued successfuly, token issued failed, Login failed etc...)

有关事件的更多信息,因此此链接

For more info about event, so this link

在身份服务器项目中,我添加了 IEventSink 接口。此接口对事件的持久性进行建模,并提供一种方法: PersistAsync 。

In the identity server project I added an implementation of the IEventSink interface. This interface models the persistence of the events and provides one method: PersistAsync.

这是cs类:

public class IdentityServerEventSink : IEventSink { private readonly IHttpContextAccessor _httpContextAccessor; private readonly UserManager<IdentityUser> _userManager; public IdentityServerEventSink(IHttpContextAccessor httpContextAccessor, UserManager<IdentityUser> userManager) { _httpContextAccessor = httpContextAccessor; _userManager = userManager; } public async Task PersistAsync(Event @event) { if (@event.Id.Equals(EventIds.ClientAuthenticationFailure) || @event.Id.Equals(EventIds.TokenIssuedSuccess) || @event.Id.Equals(EventIds.TokenIssuedFailure)) { Identity user = null; try { user = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User); if (user != null) { // do stuff } } catch (Exception ex) { // handle exception } } } }

通过DI我注入了 IHttpContextAccessor ,因此您需要在服务配置中添加以下行:

Over DI i'm injecting the IHttpContextAccessor, so you need to add this line in the services configuration:

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

此行将IEventSink实现包含在conatiner中:

And this line to include the IEventSink implementation in the conatiner:

services.AddTransient<IEventSink, IdentityServerEventSink>();

希望这会有所帮助!

更多推荐

IdentityServer 4在登录/刷新令牌时获取时间戳

本文发布于:2023-11-04 10:16:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   时间   IdentityServer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!