且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

强制用户注销

更新时间:2023-12-02 13:07:58

这真的取决于你什么缓存。如果它的数据,那么您可以清除缓存的数据,而不是迫使用户重新登录。

It really depends what you've cached. If it's data then you can clear the cached data rather than forcing your users to login again.

如果它的数据或权限/安全性的变化,那么你可以在你的数据库称为SchemaVersion存储数据库的当前版本的设置。在用户请求到应用程序的每个记录可以比较反对一个在数据库中的Cookie /会话版本,如果它不同于获取客户端删除会话/ cookie,并强制重新登录。

If it's data or permissions / security change then you could have a setting in your database called SchemaVersion that stores the current version of the database. Each logged in user request to the app could compare the cookie / session version against the one in the database and if it differs to get the client to delete the session / cookie and force a re-login.

据微软的帮助文章您可以重置会话这样的:

According to a Microsoft help article you can reset the session like this:

Session.Abandon(); 
Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));

和从MSDN可以清除您的Cookie 这样的:

And from MSDN you can clear your cookie like this:

if (HttpContext.Current.Request.Cookies["MyCookieName"] != null)
{
    HttpCookie aCookie = HttpContext.Current.Request.Cookies["MyCookieName"];
    aCookie.Expires = DateTime.Now.AddDays(-10);
    aCookie.Value = "";
    HttpContext.Current.Response.Cookies.Add(aCookie);
}

这应该强制登录,但我还没有证实这个自己。

This should force a login, but I haven't confirmed this myself.

因此​​,在总结,你可以使用ASP.NET缓存来存储数据库架构版本和:

So in summary, you can use the ASP.NET Cache to store the db schema version and:

在页面加载的开始调用一个辅助类 LoginResetHelper.IsDbValid()来看看我们是否需要重新登录

At the start of the page load call a helper class LoginResetHelper.IsDbValid() to see if we need to login again

在辅助类,你会问

if (Cache["SchemaVersion"] == null)
{
   // retrieve schemaVersion from db

   Cache.Add("SchemaVersion", schemaVersion);
}
HttpCookie oCookie = new HttpCookie("ClientSchemaVersion");
if (Cache["SchemaVersion"] == oCookie.Value)
   return true;
return false;

如果IsDbValue为真,则继续正常

If IsDbValue is true, the continue as normal

如果它是假的,然后调用 LoginResetHelper.ResetLogin()并重定向到登录页面。

If it is false, then call the LoginResetHelper.ResetLogin() and redirect to login page.

ResetLogin()您将执行我在上面