可能将Identity 2.0模型中的DateTime列默认为Null
本文关键字:DateTime 默认 Null 模型 Identity | 更新日期: 2023-09-27 18:10:48
在我的用户配置文件中有一个自定义的DateTime列。它在身份模型中是这样定义的:
public DateTime TemporaryPasswordIssueDateTime { get; set; }
默认为1/1/1900 12:00:00 AM
。是否可以允许DateTime列默认为null
?
我写这篇专栏的目的是在忘记密码。如果用户忘记了他们的密码,我使用Membership.GeneratePassword()
生成一个临时密码,通过电子邮件发送给用户。我还更新了PasswordHash和SecurityStamp。temporarypasswor劝阻时间设置为当前日期时间。
当用户尝试登录时,如果temporarypasswor劝阻时间是空的,他们登录正常使用他们的用户名/密码。
如果temporarypasswor劝阻时间不为空,那么密码被认为是临时的,我检查它是否已经过期。如果有必要,我可以创建一个布尔值来表示密码是临时的。
是可能的DateTime列为空在AspNetUsers数据库表?如果是这样,我该如何在IdentityModel.cs中设置这个?
编辑
设置DateTime列为空:
public DateTime? TemporaryPasswordIssueDateTime { get; set; }
导致这行代码:
TimeSpan ts = DateTime.Now - user.TemporaryPasswordIssueDateTime;
抛出这个错误:
Cannot implicitly convert type 'System.TimeSpan?' to 'System.TimeSpan'.
An explicit conversion exists (are you missing a cast?)
我已经在TimeSpan ts = code之前检查了空值
默认为
1/1/1900 12:00:00 AM
。是否可以允许DateTime列默认为null?
是,映射到一个可空的DateTime:
public DateTime? TemporaryPasswordIssueDateTime { get; set; }
但是你必须确保它在数据库中也是ALLOW NULL,并且所有其他层都可以一起玩。1/1/1900
不是。net中日期的null
值。即default(DateTime)
或DateTime.MinValue
。