我建议结合使用Application_AuthenticateRequest和ASP.NET缓存的,如下所示:
1)当用户将被删除,写用户ID输入到ASP.NET高速缓存,在那里可以坐的有限时间内(也许有一天):
string cacheKey = "RecentlyDeletedUserId" + userId;
Cache.Add(
cacheKey,
true,
null,
DateTime.Now.AddDays(1),
null,
CacheItemPriority.Normal,
null
);
2)在Global.asax中,您可以添加Application_AuthenticateRequest
处理程序,这是形式解雇后为每个请求认证票证被服务器成功接收。在此处理程序中,您可以制作一个便宜的内存中缓存请求,以查看该用户是否位于最近删除的用户列表中。如果是,请将其签名并重定向到登录页面。
protected void Application_AuthenticateRequest(object sender, EventArgs e) {
string cacheKey = "RecentlyDeletedUserId" + userId;
if (Cache[cacheKey] != null)
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
}
如果由于某种原因你不喜欢重定向的办法,你可以采取其他方式的做法是这样的:
protected void Application_AuthenticateRequest(object sender, EventArgs e) {
string cacheKey = "RecentlyDeletedUserId" + userId;
if (Cache[cacheKey] != null)
{
IPrincipal anonymousPrincipal = new GenericPrincipal(new GenericIdentity(String.Empty), null);
Thread.CurrentPrincipal = anonymousPrincipal;
HttpContext.Current.User = anonymousPrincipal;
}
}
这只是替换一个匿名用户的用户,这确保了用户将无法在您的网站上执行任何操作。 (这种替代方法是从Invalidating ASP.NET FormsAuthentication server side。)
谢谢你的真棒回应。我想我更喜欢缓存想法,因为我必须实现UserStillValid方法,并且我不喜欢在global.asax中运行业务逻辑的想法。 – Marko 2013-02-10 01:57:25
对不起,我削减和粘贴太多了!!第二替代UserStillValid只是检查缓存 – 2013-02-10 03:15:16
我已更新答案,以反映正确的代码。 – 2013-02-10 04:48:39