简单的会员资格:用户的上次登录日期 [英] Simple Membership:Last Login date of User
问题描述
我使用我的asp.net MVC 4应用简单的会员资格。我怎样才能得到用户的上次登录日期。我没有看到日期是默认的网页架构表产生的?我是否需要创建领域中简单的成员日期LastLogin?
谢谢
I am using the simple membership in my asp.net MVC 4 application. How i can get the Last Login date of User. I don't see the date is created in default webpages schema table? Do i need to create the field for the LastLogin date in simple Membership? Thanks
推荐答案
我已经解决了这种方式:
I have solved this way:
-
我添加了一个LastLogin领域的用户配置模型UsersContext:
I added a LastLogin field to UserProfile Model in UsersContext:
[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:
I modified the Login Method in 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像波纹管的方式:
Then I fetched UserProfiles Like the way bellow:
public ActionResult Index()
{
return View("Index",_db.UserProfiles);
}
最后,我显示日期时间LastLogin在Index.cshtml:
Finally, I displayed LastLogin DateTime in Index.cshtml:
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastLogin)
</td>
</tr>
}
这篇关于简单的会员资格:用户的上次登录日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!