在对 IdentityServer4 进行初始 ASPNET MVC 客户端身份验证后捕获事件?
Capture event after initial ASPNET MVC Client authentication to IdentityServer4?
我有一个 ASP.NET MVC 客户端连接到 IdentityServer4。
我使用 [Authorize] 装饰来启动对 IdentityServer 登录的调用,然后在成功登录后正确地重定向回我的客户端应用程序。
但在这次成功登录后,我想执行一些额外的操作(例如在客户端数据库中记录 LastLoginDateTime,更新客户端数据库中的一些角色信息等...)。
应该使用什么事件,它只会在 IdentityServer 正确验证并传递回客户端应用程序时触发?
我一直在尝试自定义 AuthorizeAttribute,但这会在每次控制器操作时触发(并且似乎也会在后续触发时丢失声明):
public class CustAuthorize : AuthorizeAttribute
{
public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
//grab information about User
var user = filterContext.HttpContext.User as ClaimsPrincipal;
var UserID_portal = (from p in user.Claims where p.Type == "sub" select p.Value).FirstOrDefault();
//perform additional database stuff here...
}
}
}
*********编辑 - 更新了答案 ***********
根据下面提供的已接受答案,如果您使用的是 Microsoft.Owin.Security.OpenIdConnect,版本=3.0.1.0,您的事件处理程序将如下所示:
Notifications = new OpenIdConnectAuthenticationNotifications()
{
SecurityTokenValidated = (context) =>
{
System.Security.Claims.ClaimsIdentity identity = context.AuthenticationTicket.Identity;
}
}
假设您使用的是 Microsoft OpenID Connect 中间件:
您可以在 OpenIdConnectOptions
上使用 Events
属性。这允许您连接到身份验证 OpenID Connect 过程中的特定事件并执行自定义逻辑。所以你会做类似的事情:
Events = new OpenIdConnectEvents {
OnTokenValidated = async x => {
/* update records */
}
}
我有一个 ASP.NET MVC 客户端连接到 IdentityServer4。
我使用 [Authorize] 装饰来启动对 IdentityServer 登录的调用,然后在成功登录后正确地重定向回我的客户端应用程序。
但在这次成功登录后,我想执行一些额外的操作(例如在客户端数据库中记录 LastLoginDateTime,更新客户端数据库中的一些角色信息等...)。
应该使用什么事件,它只会在 IdentityServer 正确验证并传递回客户端应用程序时触发?
我一直在尝试自定义 AuthorizeAttribute,但这会在每次控制器操作时触发(并且似乎也会在后续触发时丢失声明):
public class CustAuthorize : AuthorizeAttribute
{
public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
//grab information about User
var user = filterContext.HttpContext.User as ClaimsPrincipal;
var UserID_portal = (from p in user.Claims where p.Type == "sub" select p.Value).FirstOrDefault();
//perform additional database stuff here...
}
}
}
*********编辑 - 更新了答案 ***********
根据下面提供的已接受答案,如果您使用的是 Microsoft.Owin.Security.OpenIdConnect,版本=3.0.1.0,您的事件处理程序将如下所示:
Notifications = new OpenIdConnectAuthenticationNotifications()
{
SecurityTokenValidated = (context) =>
{
System.Security.Claims.ClaimsIdentity identity = context.AuthenticationTicket.Identity;
}
}
假设您使用的是 Microsoft OpenID Connect 中间件:
您可以在 OpenIdConnectOptions
上使用 Events
属性。这允许您连接到身份验证 OpenID Connect 过程中的特定事件并执行自定义逻辑。所以你会做类似的事情:
Events = new OpenIdConnectEvents {
OnTokenValidated = async x => {
/* update records */
}
}