views:

54

answers:

2

I'm building a form that will allow my site's users to set a secret question and answer. I'm using NHibernate to persist my model to our database.

public class User {
    public string Question { get; set; }
    public string Answer { get; set; }
}

I want to encrypt the input from the user before storing it in the database. My first thought was to use a backing field for both properties and perform the encryption or decryption in the getter and setters, but this felt like I was violating SoC.

Is there a better place to transform the data?

+2  A: 

You could write a custom model binder for the User class which will encrypt the input values and directly provide an instance of the User class with encrypted values to the controller action.

This encryption could also be performed inside the controller action which is handling the submission of the form.

Darin Dimitrov
I just started to think about that this morning. I have read about model binders but I have never used them, so I was not sure if this is a situation that warrants their use.
Justin R.
I decided to implement a custom modelbinder to hash the user's secret answer. Thanks!
Justin R.
+1  A: 

You can use this to have NHibernate do it transparently when persisting.

Diego Mijelshon
I might end up using this method for encrypting the user's secret question. Do you know I'd there's a way for me to perform dependency injection through an IUserType's constructor?
Justin R.
The type has a parameter, `encryptor`, where you can provide your implementation.
Diego Mijelshon