是否可以在 Asp.NET MVC 中以编程方式注销用户?我知道您可以使用:
FormsService.SignOut();
但这指的是发出请求的网页的上下文。我试图阻止用户登录两次。所以如果我打电话:
MembershipUser membershipUser = Membership.GetUser(userName);
if (membershipUser.IsOnline == true)
{
// log this user out so we can log them in again
FormsService.SignOut();
}
调用
FormsService.SignOut();
与用户的上下文无关,与另一个已经登录的网络浏览器?
最佳答案
实现此目标的一种常用方法是,在每次加载页面时,检查当前用户是否需要注销。
if (User.Identity.IsAuthenticated && UserNeedsToSignOut())
{
FormsAuthentication.SignOut(); // kill the authentication cookie:
Response.Redirect("~/default.aspx"); // make sure you redirect in order for the cookie to not be sent on subsequent request
}
你可能会担心这个方法会太慢,
"Why do I have to call this damned function each page load? It probably hits the database each time!"
它不需要很慢。您可以缓存在任何给定时间不应登录的用户列表。如果他们的用户名在该缓存中,那么他们下次访问页面时将触发注销代码。
关于asp.net - 以编程方式注销 "specific"用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3039534/