gpt4 book ai didi

asp.net-mvc-4 - 简单成员资格 :Last Login date of User

转载 作者:行者123 更新时间:2023-12-04 06:41:33 24 4
gpt4 key购买 nike

我在我的 asp.net MVC 4 应用程序中使用简单的成员资格。我如何获得用户的上次登录日期。我没有看到在默认网页架构表中创建的日期?我是否需要在简单成员(member)中为 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>
    }
  • 关于asp.net-mvc-4 - 简单成员资格 :Last Login date of User,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14321696/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com