asp.net-mvc-4 – 简单会员:用户的上次登录日期
发布时间:2020-12-16 04:25:50 所属栏目:asp.Net 来源:网络整理
导读:我在我的asp.net MVC 4应用程序中使用简单的成员资格.我如何获得用户的上次登录日期.我没有看到在默认的网页架构表中创建日期?我是否需要在简单会员资格中为LastLogin日期创建字段? 谢谢 解决方法 我这样解决了: 我在UsersContext中向UserProfile模型添加
我在我的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> } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在ASP.NET中,获取基本UR1请求的最快方法是什么?
- ASP.NET MVC – 查看主页,如何设置标题?
- asp.net-mvc – 如何在MVC 4 API中打印JSON脚本
- ASP.Net页面刷新后自动滚动到原来位置
- asp.net – 如何绑定gridview中的下拉列表?
- asp.net-mvc – ASP.NET MVC Remote属性方法参数始终传递nu
- asp.net中Session缓存与Cache缓存的区别分析
- 调试 – 如何调试经典ASP?
- asp.net-mvc-2 – MVC HttpDelete动词导致405方法不允许
- 在Classic ASP中从ADODB调用参数化Oracle查询
推荐文章
站长推荐
- asp.net-mvc-4 – 如何从asp.net mvc 4中的空Web
- asp.net – 如何确定HttpModules的执行顺序?
- asp.net-mvc – 使用URL重写时Url.Action不正确
- ASP.NET Azure中的Trace.WriteLine
- asp.net-mvc – 如何在asp.net mvc应用程序中自动
- ASP.NET – 如何有效地使用设计模式而不需要过度
- asp.net – System.Web.Cache和HTTPContext.Cure
- asp.net – 是否有可能在代码隐藏中获取不可见的
- asp.net-mvc – ASP.NET MVC DDD应用程序中的组合
- 在asp.net identity 2.0中获取分配角色的用户列表
热点阅读