c# - 当他/她的用户名被另一个用户更改时如何强制注销用户?

标签 c# asp.net-mvc forms-authentication

在我的应用程序中,我使用 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 值跟踪,如果是,则将他重定向到登录页面。

使用表单认证信息强制注销请看以下文章:

更新 2

清除 cookie

希望对你有帮助。

关于c# - 当他/她的用户名被另一个用户更改时如何强制注销用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12379215/

相关文章:

c# - XML 序列化和 MS/Mono 可移植性

c# - 为什么不注入(inject) IServiceProvider 而不是每个单独的依赖项?

c# - 如何在datagrid wpf中设置列的位置

java - 从 Java 使用 ASP.NET 表单例份验证

c# - 获取登录用户

c# - 以编程方式 WPF listbox.ItemsSource 更新

html - 将 anchor 标记的显示分成多行

c# - MVC 中 Controller 操作命名的一致性

c# - 如何在 asp net 5 中传递 "asp-for"的字符串值

asp.net - FormsAuthentication.Initialize() 的作用是什么?