Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, with another webbrowser who is logged in already?

like image 996
MD_Oppenheimer Avatar asked Jun 14 '10 17:06

MD_Oppenheimer


1 Answers

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.

like image 113
Brian Webster Avatar answered Nov 15 '22 06:11

Brian Webster