我在我的应用程序中使用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);
}
在我的客户全局过滤器中,我检查用户是否存在,如果不存在,我会将他们注销。然而,它并没有起作用。所谓的“起作用”,是指他们通过身份验证并获得访问应用程序的权限。提前感谢!
this.ControllerContext.HttpContext.Response.Cookies.Clear();
- Display Name