在存储到数据库之前,我使用IDataProtector对数据进行加密。例如,所有字符串都可以正常工作:
LastName = _protector.Protect(student.LastName)但是,我不能使用“保护”:
public DateTime EnrollmentDate { get; set; }使用:
Student nstudent = new Student
{
LastName = _protector.Protect(student.LastName),
EnrollmentDate = _protector.Protect(student.EnrollmentDate)
};我知道这个错误:
cannot convert from 'System.DateTime' to 'byte[]'发布于 2017-10-05 07:00:20
不能对固定长度的数据类型(如DateTime )进行保护/取消保护。字符串的扩展方法创建byte[]表示,对其进行加密,然后创建受保护值的base64字符串表示。这是因为受保护的字符串和不受保护的字符串可以有不同的长度。但是DateTime是一个长度固定的结构,即使使用支持long滴头计数,它也只给出4个字节。这要求您使用不同的表示形式来存储受保护的值。
所以你可以
byte[] protectedDateTime = protector.Protect(BitConverter.GetBytes(student.EnrollmentDate.Ticks));
DateTime unprotectedDateTime = new DateTime(ticks: BitConverter.ToInt64(protector.Unprotect(protectedDateTime), 0));发布于 2017-10-05 02:34:33
也许你需要用字符串代替
string sEnrollmentDate = _protector.Protect(student.EnrollmentDate.ToString())请注意,ToString将保护日期的本地化文本形式,因此最好使用一些方法来返回ISO日期字符串(参见Given a DateTime object, how do I get an ISO 8601 date in string format?)。
https://stackoverflow.com/questions/46576959
复制相似问题