Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net SimpleMembership取消用户身份验证?_.net_Asp.net Mvc 4_Simplemembership - Fatal编程技术网

.net SimpleMembership取消用户身份验证?

.net SimpleMembership取消用户身份验证?,.net,asp.net-mvc-4,simplemembership,.net,Asp.net Mvc 4,Simplemembership,我正在制作一个新的MVC4网站,我已经设置了SimpleMembership。我还创建了一个CustomPrincipal,它继承自RolePrincipal,并具有一个名为UserInfo的附加属性,该属性包含有关用户的附加信息,如LastName、FirstName和IsActive。所有这些都通过FormsAuthenticationTicket userData属性存储在cookie中 我的问题如下。假设我有一个管理页面,管理员用户可以在其中禁用其他用户的帐户-将IsActive属性设置

我正在制作一个新的MVC4网站,我已经设置了SimpleMembership。我还创建了一个CustomPrincipal,它继承自RolePrincipal,并具有一个名为UserInfo的附加属性,该属性包含有关用户的附加信息,如LastName、FirstName和IsActive。所有这些都通过FormsAuthenticationTicket userData属性存储在cookie中

我的问题如下。假设我有一个管理页面,管理员用户可以在其中禁用其他用户的帐户-将IsActive属性设置为false。假设同时被禁用的用户当前实际登录。我不希望该用户在被拒绝访问权限时能够继续浏览该站点


我怎样才能终止他的会话,也就是销毁他的FormsAuthenticationCookie?这是正确的做法还是SimpleMembership中还有其他我遗漏的东西?实现这一任务的正确途径是什么?任何建议都将不胜感激……

我建议结合使用Application_AuthenticateRequest和ASP.NET缓存,如下所示:

1) 删除用户时,将用户ID写入ASP.NET缓存中,在该缓存中,用户ID可以保留一段有限的时间(可能是一天):

2) 在global.asax中,您可以添加
应用程序\u AuthenticateRequest
处理程序,该处理程序在服务器成功接收表单身份验证票证后针对每个请求启动。在这个处理程序中,您可以发出一个廉价的内存缓存请求,以查看该用户是否在最近删除的用户列表中。如果是,您将其注销,并将其重定向到登录页面

protected void Application_AuthenticateRequest(object sender, EventArgs e) {
    string cacheKey = "RecentlyDeletedUserId" + userId;
    if (Cache[cacheKey] != null)
    {
        FormsAuthentication.SignOut();
        FormsAuthentication.RedirectToLoginPage();
    }
}
如果出于某种原因您不喜欢重定向方法,您可以采取以下方法:

protected void Application_AuthenticateRequest(object sender, EventArgs e) {
    string cacheKey = "RecentlyDeletedUserId" + userId;
    if (Cache[cacheKey] != null)
    {
        IPrincipal anonymousPrincipal = new GenericPrincipal(new GenericIdentity(String.Empty), null);
        Thread.CurrentPrincipal = anonymousPrincipal;
        HttpContext.Current.User = anonymousPrincipal;
    }     
}

这只是将用户替换为匿名用户,从而确保用户无法在您的站点上执行任何操作。(此替代方法来自。)

感谢您的精彩回复。我想我更喜欢缓存的想法,因为我必须实现UserStillValid方法,我不喜欢在global.asax中运行业务逻辑的想法。对不起,我剪切和粘贴了太多!第二个备选方案中的UserStillValid将只是检查缓存第二组代码将做什么?存储在anonymousPrincipal中的内容。
Thread.CurrentPrincipal=anonymousPrincipal和HttpContext.Current.User=anonymousPrincipal的含义是什么而不是注销?假设管理员有一个UI,管理员在其中使一个用户处于非活动状态,同时使页面中的用户处于非活动状态。那么什么时候启动应用程序_AuthenticateRequest?它会为任何页面请求触发吗?
protected void Application_AuthenticateRequest(object sender, EventArgs e) {
    string cacheKey = "RecentlyDeletedUserId" + userId;
    if (Cache[cacheKey] != null)
    {
        IPrincipal anonymousPrincipal = new GenericPrincipal(new GenericIdentity(String.Empty), null);
        Thread.CurrentPrincipal = anonymousPrincipal;
        HttpContext.Current.User = anonymousPrincipal;
    }     
}