我有一个函数有一个循环,在每个循环结束时休眠6秒
Thread.Sleep(TimeSpan.FromSeconds(6));
这循环10次,即该功能运行60秒,每次暂停6秒.
我在循环开始时进行了身份验证测试
if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
return null;
}
Run Code Online (Sandbox Code Playgroud)
所以每次它第一次认证然后运行并等待6秒.
这是我的功能:
while (counter < 10)
{
if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
return null;
}
// doing stuff
Thread.Sleep(TimeSpan.FromSeconds(6));
counter++;
}
Run Code Online (Sandbox Code Playgroud)
现在用户同时注销(比如在第15秒).我使用ajax注销,因此不想重定向我的页面.即使在注销后,IsAuthenticated对于所有10个循环始终为true,并且仅在重新执行此函数时才为false
注销我使用:
FormsAuthentication.SignOut();
Session.Abandon();
Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
HttpCookie cookie = HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);
}
Run Code Online (Sandbox Code Playgroud)
但仍然是真的.. 我想在用户注销后立即停止执行我的线程
发生这种情况是因为它IsAuthenticated具有内部缓存,因为一次又一次地进行此身份验证的时间过于昂贵.因此,在你的循环中,离开页面时,IsAuthenticated不会改变.
另一方面,这是什么意思?在一个循环中用户可以看到前4个认为,然后看不到剩下的因为没有更多的认证?没意义.
如果你想检查用户是否离开并离开页面,你可以做的是检查一些其他参数.
这是显示此内部缓存的代码.
public virtual bool IsAuthenticated
{
get
{
if (this.m_isAuthenticated == -1)
{
WindowsPrincipal principal = new WindowsPrincipal(this);
SecurityIdentifier sid = new SecurityIdentifier(IdentifierAuthority.NTAuthority, new int[] { 11 });
this.m_isAuthenticated = principal.IsInRole(sid) ? 1 : 0;
}
return (this.m_isAuthenticated == 1);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3234 次 |
| 最近记录: |