我在我的 asp.net MVC 4 应用程序中使用简单成员资格。我如何获取用户的上次登录日期。我没有看到默认网页架构表中创建的日期?我是否需要在简单会员资格中创建 LastLogin 日期字段?
谢谢
我已经这样解决了:
-
我在 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>
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)