如何在角色更改后注销特定用户?

时间:2016-01-02 14:21:14

标签: c# asp.net-mvc entity-framework-4

在我正在处理的项目中,管理员可以删除用户发布的评论。删除评论后,我调用一个函数来检查用户是否仍有足够的评论来访问网站的特定部分。

如果用户没有足够的评论,他的角色会更改并调用此方法:

    public void AfterRoleChanges(string c)
    {
        var authenticationManager = System.Web.HttpContext.Current.GetOwinContext().Authentication;
        authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);

        var user = UserManager.FindById(c);
        var identity = UserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

        authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity);
    }

其中c = userId。但问题是,如果管理员点击删除按钮,管理员会以该用户身份登出并登录,因为身份验证管理员只能从“当前”上下文调用。

有没有办法获得特定userId的身份验证管理器?

1 个答案:

答案 0 :(得分:0)

HttpContext.Current有一种HttpContext。您可以在静态类中定义列表或字典,以在登录时存储上下文,并在注销时将其丢弃。通过访问列表,您可以访问所有用户并按需搜索。

建议使用词典,因为您可以使用userID作为键存储上下文。

希望它有所帮助。