Shweta Saxena Shweta Saxena - 3 months ago 50
C# Question

Encrypting Decrypting Data via Entity Framework using separate class exposing encrypted/decrypted values: Linq statements fails

In addition to my earlier question on entity framework. My purpose is to encrypt the field before saving to DB and Decrypt the fields before reading from DB.I have class (User.cs) generated from table User(having UserName property(to be encrypted) and I created a separate class SecureUser with UserName property that isencrypted/decrypted as suggested earlier. But I am not sure how to map this new SecureUser class to DB and not the previous POCO class User. My Linq queries fail when I replace UserName with UserName from SecureUser class. I tried doing the same thing with partial classes , same thing happens.
Any suggestions will be greatly appreciated !

[Table("Users")]
public class User
{

#region database table column mapped fields
[Key]
[Required]
public Int32 UserID { set; get; }

[Required]
[MaxLength(50)]
public String UserName { set; get; }

[Required]
public Int32 CustID { set; get; }

//created the separate class as
public class SecureUser // UserViewModel
{
//
private readonly User _user;

public SecureUser(User user)
{
_user = user;
}

public string UserName
{
get { return Decrypt(_user.UserName); }
set { _user.UserName = Encrypt(value); }
}


}

Answer

On adding the user object to the dbcontext, you should set UserName=Encrypt(model.UserName) where model is the object returned by the user for adding it

// before adding the model to the dbcontext
model.UserName=Encrypt(mode.UserName);
db.Users.Add(model);
db.SaveChanges();

[Table("Users")]
public class User
{
    #region database table column mapped fields
    [Key]
    [Required]
    public Int32 UserID { set; get; }

    [Required]
    [MaxLength(50)]
    public String UserName { set; get; } // this field will be encrypted field

    [Required]
    public Int32 CustID { set; get; }

    [NotMapped]
    public string DecryptedUserName
    {
        get { return Decrypt(UserName); }  // this you can display it to the user
    }
}

to use the NotMapped, you should add this line in the using statements

using System.ComponentModel.DataAnnotations.Schema;

hope this will help you