在我的应用程序中,我使用 Forms-Authentication 来登录和注销用户。
一个功能是管理员可以更改其他用户的用户名。在这种情况下,我需要注销更改了用户名的用户。
如果我不这样做,由于他们之前设置的 cookie,他们将获得对应用程序的访问权限并收到错误消息(因为他们的用户名不存在,并且在某些部分我使用他们的用户名来实现某些功能)。
如何使用 Forms-Authentication 强制这些用户注销?
更新:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string controller = filterContext.RouteData.Values["controller"].ToString();
string action = filterContext.RouteData.Values["action"].ToString(); ;
// Below returns the previous username, which does not exist anymore in db.
string userName = HttpContext.Current.User.Identity.Name;
UnitOfWork unitOfWork = new UnitOfWork();
if (!unitOfWork.UserRepository.UserExists(userName))
{
FormsAuthentication.SignOut();
filterContext.HttpContext.Session.Clear();
filterContext.HttpContext.Session.Abandon();
// I am not using Roles.
}
unitOfWork.Dispose();
base.OnActionExecuting(filterContext);
}
在我的客户全局过滤器中,我检查用户是否存在,如果不存在我将他们注销。但是,它不起作用。我所说的工作是指他们通过身份验证并获得对应用程序的访问权限。
提前致谢。
最佳答案
这是强制用户退出的方法:
public void UserPasswordChangedHandler()
{
FormsAuthentication.SignOut();
Roles.DeleteCookie();
Session.Clear();
}
我认为不需要逐行解释,它的 self 解释就足够了。 如果我弄错了,请告诉我。
更新
对您的附加问题的直接回答是,如果管理员更新了他的数据,则对每个用户进行 bool 值跟踪,如果是,则将他重定向到登录页面。
使用表单认证信息强制注销请看以下文章:
- How can I force a user to log out
- How can I force a log out of all users for a website ,
- ASP.NET forms authentication forced logout
- How to log off multiple MembershipUsers that are not the current user
更新 2
清除 cookie
- HowTo: create and remove Cookies with ASP.NET MVC
- How do you clear cookies in ASP NET MVC 3 and C#
- How do I invalidate a bad authentication cookie
希望对你有帮助。
关于c# - 当他/她的用户名被另一个用户更改时如何强制注销用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12379215/