![](/img/trans.png)
[英]asp:DropDownList Linq-to-sql data source binding text field to relation-property value
[英]Encrypt field value in LINQ-to-sql
我正在嘗試采用更好的加密方法來實現以下功能
Person.FirstName = Encrypt(rawFirstName);
Person.sensitiveInfo = Encrypt(sensitiveInfo);
我寧願不必每次使用時都對這種數據進行手動加密,因此我研究了修改.dbml和.designer文件中找到的Person對象的方法。
我想出了這個:
.dbml文件:
<Column Name="FirstName" Type="System.String" DbType="VarChar(256) NOT NULL" CanBeNull="false" />
.designer.cs文件:
public partial class Person : INotifyPropertyChanging, INotifyPropertyChanged
{
private string _FirstName;
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_FirstName", DbType="VarChar(256) NOT NULL", CanBeNull=false)]
public string FirstName
{
get
{
return Decrypt(this._firstName);
}
set
{
if (Decrypt(this._firstName) != value)
{
this.OnFirstNameChanging(value);
this.SendPropertyChanging();
this._firstName = Encrypt(value);
this.SendPropertyChanged("FirstName");
this.OnFirstNameChanged();
}
}
}
}
我現在面臨的問題是它可以在程序內部運行(為了確保安全,我已經記錄了set
的before / after值),但是實際上會將未加密的值保存到數據庫中。 好像我想的那樣,存儲_FirstName
變量沒有保存到數據庫中,但是使用了get
方法。
我已經采取了看看這個答案 ,以及這一個 ,但我不能為我的生活究竟弄清楚他們試圖與保護VS內部變量獲得跨越東西。 有什么簡單的我想念的嗎?
最好的方法是對生成的類使用部分類。 存儲在數據庫中的字段應為FirstNameSecure,LastNameSecure等。
然后在部分類中創建屬性FirstName,LastName等,這些屬性將加密/解密其安全版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.