ASP.NET MVC How to be logout when same user is logged in from different computer

Gökhan Özdemir

I did a lot of research but I couldn't find a clear solution. If logging in with the same user from a different computer, the old login must be logout.

My controller action code:

[HttpPost]
public ActionResult Index(User usr)
{
    var response = LoginManager.Login(usr);
    if (response != null)
    {
        Session.Add(Session.CurrentUser, response);
        return RedirectToAction("index", "Home");
    }
    return View();
}

LoginManager.Login code:

[HttpPost]
public static User Login(User usr)
{
    using (var db = new Entities())
    {
        var data = db.Users.FirstOrDefault(s => s.Email == usr.Email && s.Pass == 
        usr.Pass);
        if (data != null)
        {
             var ip = HttpContext.Current.Request.UserHostAddress;
             var addLog = new Log {UserID = data.Id, Date = DateTime.Now, IP = ip};
             db.Logs.Add(addLog)
             db.SaveChanges();

             return data;
        }
        return null;
    }
}

Session.CurrentUser code:

namespace ADMIN.Project
{
    public class Session
    {
        public const string CurrentUser = "CurrentUser";
    }
}
Gökhan Özdemir

Solution! it work fine.

Layout.cshtml Trigger

 var ip = HttpContext.Current.Request.UserHostAddress;
 var controlip= HomeManager.GetLogIP(loginuser.Id);
    if (controlip.IP != ip)
    {
        Session.Abandon();

        Response.Redirect("/Login/Index?msg=logged in from different computer");
    }

Manager GetLogIP

 public static Log GetLogIP(int id)
 {
   using (var db = new Entities())
   {
      var value = db.Logs.Where(x => x.UserID == id).ToList().Last();

      return value;
   }
 }

LoginController

 [HttpGet]
 public ActionResult Index(string msg)
 {
     ViewBag.Warning= msg;

     return View();
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to logout user in OWIN ASP.NET MVC5

Identifying the logged in user from different browser tab in asp.net

How to get items for logged in user ASP.NET MVC

How to logged out the user if they logged out from one browser on same computer in servlet

TestCafe: How to test logout if user logged in from another machine

ASP.NET MVC: Get user id of currently logged in user

asp.net mvc6 Authorization redirect do not work when user is not logged

CustomAuthorizeAttribute redirects to LoginView even when the user is logged in (ASP.Net MVC 5)

How to get logged user data in ASP.NET Core 5 MVC (.NET 5)?

How to handle different category of logged in user in asp.met mvc application

Error getting products from the logged in user. ASP.NET Core MVC

Spring security logout - add a message only when logout triggered from a logged in user

Prevent user to go back after logout in asp.net mvc

How to obtain the name of the current user logged into my ASP.NET MVC website?

How to authorize user who logged in using external login in Asp.Net MVC

How can keep the user logged in even after the browser has closed with Identity ASP.NET MVC framework?

How to get the currently logged in user name through an interface to insert in updated by column in asp.net core mvc?

Get Logged in User Role: Asp.net MVC5

Get UserID of logged-in user in Asp.Net MVC 5

ASP.NET MVC FormsAuthentication check if user logged in

ASP.NET MVC Caching - Logged-in user gets cached

How to detect the logged in user from Keycloak OpenID logout_token on back channel logout?

Tow types of user logged in the same login form - ASP.NET

How to logout a user when a new user logs in in the same browser?

How to restrict user from same Username and Password to logic from two system/browser in asp.net mvc 4.0?

How to permanently logout the user from my asp.net core application?

Flutter : How to keep user logged in and make logout

How to retrieve user logged on by reading computer name from database?

How to check if user is logged in to ASP.NET Core web application when using ASP.NET Core Web API to house identity