加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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>
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读