开发者

programmatically logout a "specific" user

Is it possible in Asp.NET MVC to proframmatically logout a user? I am aware that you can use:

FormsService.SignOut();

But this refers to the context of the webpage making the request. I'm trying to prevent a user logging in twice. So if I call:

MembershipUser membershipUser = Membership.GetUser(userName);
if (membershipUser.IsOnline == true)
{
    //  log this user out so we can log them in again
    FormsService.SignOut();    
}

Calling FormsService.SignOut(); has no bearing on the context of the user say, wit开发者_如何学Ch another webbrowser who is logged in already?


One common method to accomplish this goal is to, on each page load, check whether the current user needs to be signed out.

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
}

You may be concerned that this method will be too slow,

"Why do I have to call this damned function each page load? It probably hits the database each time!"

It doesn't need to be slow. You may cache a list of users that should not be signed in at any given time. If their username is in that cache, then the sign out code will be triggered next time they access a page.


Take a look at this blog How to allow only one user per account in ASP.Net

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜