在一个MVC项目中,我实现了基于cookie的asp.net身份。现在,我要求在auth会话过期或用户注销时向远程服务发出请求。
有没有任何自然的方法来实现这一目标?现在我设法从CookieAuthenticationProvider设置两个委托属性,如下所示:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Login"),
CookieSecure = CookieSecureOption.SameAsRequest,
ExpireTimeSpan = TimeSpan.FromMinutes(expireInMinutes),
CookiePath = cookiePath,
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = c =>
{
if (c.Properties.ExpiresUtc.HasValue && c.Properties.ExpiresUtc.Value < c.Options.SystemClock.UtcNow)
{
c.RejectIdentity();
c.Request.Context.Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
return Task.FromResult(0);
}
if (c.Options.SlidingExpiration)
{
// Reissue the auth cookie
}
return Task.FromResult(0);
},
OnResponseSignOut = c =>
{
// Make a custom request
}
}
});
乍一看它看起来很有效,但我不喜欢在这里检查有效期的想法。问题是,当auth cookie刚刚过期时,不会调用OnResponseSignOut,但仅在我显式调用IAuthenticationManager.SignOut时调用。
创建自定义CookieAuthenticationProvider是这里的最佳选择,还是可能有另一个干净自然的解决方案?