我在我的 asp.net MVC 4 应用程序中使用简单的成员资格。我如何获得用户的最后登录日期。我没有在默认网页架构表中看到创建日期?我是否需要在简单的会员资格中为 LastLogin 日期创建字段?谢谢
			
			7690 次
		
1 回答
            11        
        
		
我已经这样解决了:
- 我在 UsersContext 中的 UserProfile 模型中添加了 LastLogin 字段: - [Table("UserProfile")] public class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public virtual string UserName { get; set; } public virtual DateTime? LastLogin { get; set; } }
- 我修改了 AccountController 中的登录方法: - public ActionResult Login(LoginModel model, string returnUrl) { if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)) { using (UsersContext db=new UsersContext()) { UserProfile userProfile = db.UserProfiles.SingleOrDefault(u=>u.UserName==model.UserName); userProfile.LastLogin = DateTime.Now; db.Entry(userProfile).State=EntityState.Modified; db.SaveChanges(); } return RedirectToLocal(returnUrl); } // If we got this far, something failed, redisplay form ModelState.AddModelError("", "The user name or password provided is incorrect."); return View(model); }
- 然后我像下面这样获取 UserProfiles: - public ActionResult Index() { return View("Index",_db.UserProfiles); }
- 最后,我在 Index.cshtml 中显示了 LastLogin DateTime: - @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.UserName) </td> <td> @Html.DisplayFor(modelItem => item.LastLogin) </td> </tr> }
于 2013-04-03T17:10:53.713   回答