Hi guys, I am using MVC 5. I want to display last login time activity.
Here is my logic program code:
In IdentityModels.cs
......................... ......................... public virtual UserProfileInfo UserProfileInfo { get; set; } public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here return userIdentity; } } public class UserProfileInfo { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime Logged { get; set; } } public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } public DbSet<UserProfileInfo> UserProfileInfo { get; set; } ......................... .........................
In AccountController.cs, Which I modified Login method:
.........................
.........................
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return View(model); } ApplicationDbContext db = new ApplicationDbContext(); UserProfileInfo logged = db.UserProfileInfo.SingleOrDefault(); logged.Logged = DateTime.Now; db.Entry(logged).State = EntityState.Modified; db.SaveChanges(); // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, change to shouldLockout: true var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); switch (result) { case SignInStatus.Success: return RedirectToLocal(returnUrl); case SignInStatus.LockedOut: return View("Lockout"); case SignInStatus.RequiresVerification: return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); case SignInStatus.Failure: default: ModelState.AddModelError("", "Invalid login attempt."); return View(model); } }
.....................
.....................
I run it and it is showed current login time. i want Last login time.
Any Idea?
Hi,
If you want to get the LastLogin , get the record for the particular user with LoginTime desc , then take the 2 record ,,since first record is for Current login time , 2nd record would be the last login time,
if you doing in sql please use the below query
with logindetail
as
(
select *,ROW_NUMBER()OVER(ORDER BY logindate DESC) row_num from UserLogin Where UserId = 2
)
select * from logindetail where row_num = 2
I do not understand. I am using asp.net MVC 5 Code first.
Hi adnan,
For this requirement, you could get the Logged value after succeed validate and store it in the session, then update the Logged value to current time.
Best Regards
Starain
Can you please send me a sample of code?
change this (is this even working with singleordefault? it would always give null if there is more than 1 item right?):
ApplicationDbContext db = new ApplicationDbContext(); UserProfileInfo logged = db.UserProfileInfo.SingleOrDefault(); logged.Logged = DateTime.Now; db.Entry(logged).State = EntityState.Modified; db.SaveChanges();
to:
ApplicationDbContext db = new ApplicationDbContext(); var loggedList = db.UserProfileInfo.ToArray(); var lastLogged = loggedList[1]; var logged = loggedList[0]; logged.Logged = DateTime.Now; db.Entry(logged).State = EntityState.Modified; db.SaveChanges();
lastLogged is your last login i guess. you can add something like var loggedList = db.userprofileinfo.Take(2).ToArray(); when your table is going to grow (allot) this is going to give you a great performance boost.
Hi adnan,
Please refer to this code below:
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); switch (result) { case SignInStatus.Success: { ApplicationDbContext db = new ApplicationDbContext(); UserProfileInfo logged = db.UserProfileInfo.SingleOrDefault(); Session["lastLoggedTime"]=logged.Logged; logged.Logged = DateTime.Now; db.Entry(logged).State = EntityState.Modified; db.SaveChanges(); return RedirectToLocal(returnUrl); }
Last logged: @session["lastLoggedTime"]
Best Regards
Starain
Work like a charm.
Many thanks